我正在用CrawlerProcess
并行地启动几个spider,就像那样。你知道吗
def main():
# ----- This part launch all given spiders ----- #
process = CrawlerProcess(get_project_settings())
process.crawl(FirstSpider)
process.crawl(SecondSpider)
process.crawl(ThirdSpider)
process.crawl(EtcSpider)
process.start() # the script will block here until the crawling is finished
所有spider都基于CSV输入文件,其中包含可在网站上查找的信息。以下是一个示例:
class FirstSpider(scrapy.Spider):
name = "first_bot"
def start_requests(self):
base_url = "https://example.fr/catalogsearch/result/?q="
script_dir = osp.dirname(osp.realpath(__file__))
file_path = osp.join(script_dir, 'files', 'to_collect_firstbot.csv')
input_file = open(file_path, 'r', encoding="utf-8", errors="ignore")
reader = csv.reader(input_file)
for row in reader:
if row:
url = row[0]
absolute_url = base_url + url
print(absolute_url)
yield scrapy.Request(
absolute_url,
meta={
"handle_httpstatus_list": [302, 301, 502],
},
callback=self.parse
)
它工作,但我可能不得不修改输入文件名,这是每个蜘蛛记录。你知道吗
是否可以在所有spider脚本上保留一个默认的“定制”文件,然后将其保存到核心.py文件(启动所有spider),如果需要,修改CSV输入文件(在本例中,所有spider的文件和名称都相同)?你知道吗
crawl接受参数,您可以在spider的from_crawler内使用它们。你知道吗
你可以把论点传给你的蜘蛛爬行,我认为这是你需要让这个工作。你知道吗
将代码更改为:
现在,在启动spider时,只需在进程爬网调用中将它们作为参数传递:
检查第三个调用是否没有设置
file_name
参数,这意味着spider将使用spider代码中指定的默认参数:相关问题 更多 >
编程相关推荐