我在一個Scrapy項目中有兩個蜘蛛。 Spider1抓取頁面或整個網站的列表並分析內容。 Spider2使用Splash在Google上獲取網址並將該列表傳遞給Spider1。如何從另一個開始Scrapy蜘蛛
所以,Spider1抓取和分析內容,並可以在不被Spider2
# coding: utf8
from scrapy.spiders import CrawlSpider
import scrapy
class Spider1(scrapy.Spider):
name = "spider1"
tokens = []
query = ''
def __init__(self, *args, **kwargs):
'''
This spider works with two modes,
if only one URL it crawls the entire website,
if a list of URLs only analyze the page
'''
super(Spider1, self).__init__(*args, **kwargs)
start_url = kwargs.get('start_url') or ''
start_urls = kwargs.get('start_urls') or []
query = kwargs.get('q') or ''
if google_query != '':
self.query = query
if start_url != '':
self.start_urls = [start_url]
if len(start_urls) > 0:
self.start_urls = start_urls
def parse(self, response):
'''
Analyze and store data
'''
if len(self.start_urls) == 1:
for next_page in response.css('a::attr("href")'):
yield response.follow(next_page, self.parse)
def closed(self, reason):
'''
Finalize crawl
'''
爲Spider2
# coding: utf8
import scrapy
from scrapy_splash import SplashRequest
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
class Spider2(scrapy.Spider):
name = "spider2"
urls = []
page = 0
def __init__(self, *args, **kwargs):
super(Spider2, self).__init__(*args, **kwargs)
self.query = kwargs.get('q')
self.url = kwargs.get('url')
self.start_urls = ['https://www.google.com/search?q=' + self.query]
def start_requests(self):
splash_args = {
'wait:': 2,
}
for url in self.start_urls:
splash_args = {
'wait:': 1,
}
yield SplashRequest(url, self.parse, args=splash_args)
def parse(self, response):
'''
Extract URLs to self.urls
'''
self.page += 1
def closed(self, reason):
process = CrawlerProcess(get_project_settings())
for url in self.urls:
print(url)
if len(self.urls) > 0:
process.crawl('lexi', start_urls=self.urls, q=self.query)
process.start(False)
的代碼被稱爲可使用當運行Spider2我有這樣的錯誤:twisted.internet.error.ReactorAlreadyRunning
和Spider1是在沒有URL列表的情況下調用。 我嘗試使用Scrapy文檔建議的CrawlRunner,但它是同樣的問題。 我嘗試使用CrawlProcess裏面的解析方法,它「工作」,但是,我仍然有錯誤消息。在解析方法中使用CrawlRunner時,它不起作用。
我不知道,但我認爲蜘蛛必須在兩個不同的文件 –
他們在分開的文件。 –