Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在使用Scrapy进行刮削之前,请检查文件中是否有URL_Python_Scrapy_File Read - Fatal编程技术网

Python 在使用Scrapy进行刮削之前,请检查文件中是否有URL

Python 在使用Scrapy进行刮削之前,请检查文件中是否有URL,python,scrapy,file-read,Python,Scrapy,File Read,我正在抓取一个包含URL列表的大文件。显然,我不能连续地刮取所有URL。我当前的解决方案从文件中读取URL。一旦它从该页面抓取和下载文档,我将写入一个新文件,该文件如下所示: https://url_i_completed_crawling E:/location_I_stored_crawled_files https://another_url_i_completed_crawling E:/another_location_I_stored_crawled_files 我

我正在抓取一个包含URL列表的大文件。显然,我不能连续地刮取所有URL。我当前的解决方案从文件中读取URL。一旦它从该页面抓取和下载文档,我将写入一个新文件,该文件如下所示:

https://url_i_completed_crawling     E:/location_I_stored_crawled_files
https://another_url_i_completed_crawling     E:/another_location_I_stored_crawled_files
我的问题是,当我停止爬行器并尝试继续我停止的地方时,程序从URL的原始文本文件开始,并开始重新爬网并用相同的内容覆盖以前的下载

我试图将代码放入spider中,检查传递到解析函数的URL是否在“completed_urls.txt”文件中。。。但很明显,随着完成的URL数量的增加,这是一个漫长的检查

所以我的问题是:当我重新启动程序时,我如何记住哪一个URL是最后一个要爬网的URL,并让我的爬行器从文本文件中的下一个URL开始

    # file containing urls to crawl is passed in from command line
    # > scrapy crawl fbo-crawler -a filename=FBOSpider/urls_file.txt   
    def __init__(self, filename=None):
        if filename:
            with open(filename, 'r') as r:
                # here I want to check if r.readlines() is passing a URL that I have aleady crawled
                # crawld URLs are stored in a text file as shown above
                self.start_urls = r.readlines()

将这样的表格格式存储到表格中可能是一个好主意。为此,建议使用关系数据库。由于索引,访问数据的速度更快。但是,在您的情况下,从原始文件中删除刮取的URL可能会有所帮助。

根据刮取文档:
Scrapy支持开箱即用的功能。

Scrapy和DELTAFETCH

这是一个可以忽略对页面的请求的粗糙的spider中间件 包含在同一个爬行器之前的爬行中看到的项目,因此 生成仅包含新项的“增量爬网”

首先,使用pip安装DeltaFetch:

pip install scrapy-deltafetch
然后,必须在项目的settings.py文件中启用它:

SPIDER_MIDDLEWARES = {
'scrapy_deltafetch.DeltaFetch': 100,
}
DELTAFETCH_ENABLED = True
重置DeltaFetch 如果要重新刮取页面,可以通过将DeltaFetch_reset参数传递给spider来重置DeltaFetch缓存:

scrapy crawl test -a deltafetch_reset=1
有关更多信息,请查看上的项目页面: