在启动CrawlerProcess/Scrapy时修改spider的CSV文件输入

2024-06-16 10:02:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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的文件和名称都相同)?你知道吗


Tags: 文件urldefscriptprocessstartreaderfile
2条回答

crawl接受参数,您可以在spider的from_crawler内使用它们。你知道吗

你可以把论点传给你的蜘蛛爬行,我认为这是你需要让这个工作。你知道吗

将代码更改为:

class FirstSpider(scrapy.Spider):
    name = "first_bot"

    file_name = 'to_collect_firstbot.csv' # <- we are gonna change this variable later

    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', self.file_name) # here we use the argument
        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时,只需在进程爬网调用中将它们作为参数传递:

def main():

    #   - This part launch all given spiders   - #

    process = CrawlerProcess(get_project_settings())

    process.crawl(FirstSpider, file_name='custom_file1.csv')
    process.crawl(SecondSpider, file_name='custom_file2.csv')
    process.crawl(ThirdSpider)
    process.crawl(EtcSpider, file_name='custom_file_whatever.csv')

    process.start()  # the script will block here until the crawling is finished

检查第三个调用是否没有设置file_name参数,这意味着spider将使用spider代码中指定的默认参数:

file_name = 'to_collect_firstbot.csv'

相关问题 更多 >