Первая очистка выполняется идеально, но когда я запускаю вторую очистку, у меня появляется ошибка ReactorNotRestartable.
Вот мой код (я упростил педагогическую речь и удалил элементы Кварто):
Код: Выделить всё
import scrapy
from scrapy.crawler import CrawlerProcess
import nest_asyncio
import json
Код: Выделить всё
class ScraperQuotesToScrapeSpider(scrapy.Spider):
name = 'scraper_quotes_to_scrape'
allowed_domains = ['https://quotes.toscrape.com']
start_urls = ['https://quotes.toscrape.com/']
def parse(self, response):
quotes_elements = response.css('div.quote')
for quote_element in quotes_elements:
author = quote_element.css('small.author::text').get()
quote = quote_element.css('span.text::text').get()
tags = quote_element.css('div.tags a.tag::text').getall()
quotes = {
'author': author,
'quote': quote,
'tags': tags
}
yield quotes
Код: Выделить всё
nest_asyncio.apply()
process = CrawlerProcess(
settings={
"FEEDS": {
"quotes.json": {"format": "json", "overwrite": "True"},
},
}
)
process.crawl(ScraperQuotesToScrapeSpider)
process.start()
Код: Выделить всё
with open('quotes.json', 'r') as f:
for line in f:
print (line)
Код: Выделить всё
class ScraperQuotesToScrapeSpider(scrapy.Spider):
name = 'scraper_quotes_to_scrape'
allowed_domains = ['quotes.toscrape.com']
start_urls = ['https://quotes.toscrape.com/']
def parse(self, response):
print("Processing ", response.url)
quotes_elements = response.css('div.quote')
for quote_element in quotes_elements:
author = quote_element.css('small.author::text').get()
quote = quote_element.css('span.text::text').get()
tags = quote_element.css('div.tags a.tag::text').getall()
quotes = {
'author': author,
'quote': quote,
'tags': tags
}
yield quotes
next_page = response.css('.next a::attr(href)').get()
# domain = self.start_urls[0][0:len(self.start_urls[0])-1]
# next_page = domain + next_page
# print("next page = ", next_page)
if next_page is not None:
yield response.follow(next_page, self.parse)
Код: Выделить всё
nest_asyncio.apply()
process = CrawlerProcess(
settings={
"FEEDS": {
"quotes.json": {"format": "json", "overwrite": "True"},
},
}
)
process.crawl(ScraperQuotesToScrapeSpider)
process.start()
with open('quotes.json', 'r') as f:
for line in f:
print (line)
Итак, мой вопрос: как остановить первый экземпляр паука после его выполнения?
Я предполагаю, что не могу использовать его повторно, потому что я изменил класс Scrapy.
Тогда я не могу комментировать своего первого паука и его выполнение, чтобы правильно отобразить мой кварто страница.
Наконец, я подумывал об уничтожении первого процесса-паука перед запуском второго процесса-паука, но не нашел подходящего способа сделать это (я представлял простой процесс.stop(), но нет).>
Подробнее здесь: https://stackoverflow.com/questions/798 ... in-rstudio
Мобильная версия