[英]Python: With Scrapy Script- Is this the best way to scrape urls from forums?
我想做的事:
由于以下原因,我可以暂停/恢复我的脚本: http : //doc.scrapy.org/en/latest/topics/jobs.html
我已经设置了脚本,因此它将每1,000,000条记录拆分一次。
而且Python字典仅检查url键中每个文本文件中是否存在重复项。 因此,至少,每个文件中的网址都是唯一的。 如果我有更大的字典。 这将大大减慢IMO的进程。 具有1个重复项(每1,000,000个日志)比数千个更好。
这是我当前正在使用的Python脚本代码:
from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor from scrapy.item import Item, Field class MyItem(Item): url=Field() f=open("items0"+".txt","w") num=open("number.txt","w") class someSpider(CrawlSpider): name = "My script" domain=raw_input("Enter the domain:\\n") allowed_domains = [domain] starting_url=raw_input("Enter the starting url with protocol:\\n") start_urls = [starting_url] i=0 j=0 dic={} global f rules = (Rule(LxmlLinkExtractor(allow_domains=(domain)), callback='parse_obj', follow=True),) def parse_obj(self,response): for link in LxmlLinkExtractor(allow_domains=(self.domain)).extract_links(response): item = MyItem() item['url'] = link.url if self.dic.has_key(item['url']): continue global f global num f.write(item['url']+"\\n") self.dic[item['url']]=True self.i+=1 if self.i%1000000==0: self.j+=1 f.close() f=open("items"+str(self.j)+".txt","w") num.write(str(self.j+1)+"\\n")
有人有更好的刮擦方法吗?
您估计这样的网站会从我的脚本中获取多少日志文件?
Scrapy下降的重复请求DUPEFILTER_CLASS ,默认设置为RFPDupeFilter ,这就好比你的方法类似,但没有见过的网址保存到多个文件。
我已经创建了一个POC。
# -*- coding: utf-8 -*-
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
class ExampleSpider(CrawlSpider):
name = "ExampleSpider"
allowed_domains = ["www.example.com", "www.iana.org"]
start_urls = (
'http://www.example.com/',
)
rules = (Rule(LxmlLinkExtractor(allow_domains=allowed_domains), callback='parse_obj', follow=True),)
log_file = open('test.log', 'a')
def parse_obj(self, response):
#self.logger.info(response.url)
self.logger.info(self.settings['DUPEFILTER_CLASS'])
self.log_file.write(response.url + '\n')
使用scrapy crawl ExampleSpider -s DUPEFILTER_DEBUG=1
运行它,应该有一些调试信息,如下所示。
[scrapy]调试:已过滤的重复请求:<GET http://www.iana.org/about/framework >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.