文件输入到scrapy给出问题

时间:2017-02-09 12:31:55

标签: python python-3.x scrapy

我正在尝试将文件输入scrapy进行处理。但是我不知道为什么我会在文件格式中输入问题。这是我试过的:

with open("url.txt","r") as f:

    DOMAIN = [u.strip() for u in f.readlines()]
    print DOMAIN
    URL = 'http://%s' % DOMAIN

class MySpider(scrapy.Spider):
    name = "emailextractor"
    allowed_domains = [DOMAIN]
    start_urls = [
        URL
    ]

输入文件采用以下格式:

emaxple.com
example.net
example.org.... etc

如何以我正在使用的格式为scrapy提供输入。我正在尝试将http://附加到我要提供的所有网址上。即使文件的GB也非常大。那么我应该做的最好的事情是什么?请帮助我 这个问题对我不起作用:Pass input file to scrapy containing a list of domains to be scraped

1 个答案:

答案 0 :(得分:0)

如果您想根据文件中的网址(或您无法直接在start_urls列表中设置的其他内容)生成请求,则必须覆盖scrapy.Spider的{​​{3你自己的蜘蛛中的方法。

在此方法中,您必须为从输入文件中读取的URL生成请求:

class MySpider(scrapy.Spider):
    name = "emailextractor"

    def start_requests(self):
        with open('urls.txt') as urls_file:
            for url in urls_file:
                yield scrapy.Request(url.strip(), callback=self.parse)

    def parse(self, response):
        # parse the pages that your spider downloaded and extract the data