调试Scrapy过程中的心得体会
2016-10-17 17:51
447 查看
1.大量抓取网页时出现“Memory Error”解决办法:设置一个队列,每当爬虫空闲时才向队列中放入请求,例如:
More info on the spider_idle signal: http://doc.scrapy.org/en/latest/topics/signals.html#spider-idle
More info on debugging memory leaks: http://doc.scrapy.org/en/latest/topics/leaks.html
P.S.还有一种限定爬取深度的方法(貌似在settings.py中?)待研究
2.如果请求的url不存在(404),则不会有response对象返回,爬虫什么也没做
3.编码问题
pubmed_spider.py中
保证抓取到的数据是utf8格式的
pipeline.py中file = codecs.open('/%s.txt' % (item['name']), mode = 'w',encoding='utf-8')将数据以utf8格式存储
from scrapy import signals, Spider from scrapy.xlib.pydispatch import dispatcher class ExampleSpider(Spider): name = "example" start_urls = ['http://www.example.com/'] def __init__(self, *args, **kwargs): super(ExampleSpider, self).__init__(*args, **kwargs) # connect the function to the spider_idle signal dispatcher.connect(self.queue_more_requests, signals.spider_idle) def queue_more_requests(self, spider): # this function will run everytime the spider is done processing # all requests/items (i.e. idle) # get the next urls from your database/file urls = self.get_urls_from_somewhere() # if there are no longer urls to be processed, do nothing and the # the spider will now finally close if not urls: return # iterate through the urls, create a request, then send them back to # the crawler, this will get the spider out of its idle state for url in urls: req = self.make_requests_from_url(url) self.crawler.engine.crawl(req, spider) def parse(self, response): pass
More info on the spider_idle signal: http://doc.scrapy.org/en/latest/topics/signals.html#spider-idle
More info on debugging memory leaks: http://doc.scrapy.org/en/latest/topics/leaks.html
P.S.还有一种限定爬取深度的方法(貌似在settings.py中?)待研究
2.如果请求的url不存在(404),则不会有response对象返回,爬虫什么也没做
3.编码问题
pubmed_spider.py中
import sys reload(sys) #python默认环境编码时ascii sys.setdefaultencoding("utf-8")
保证抓取到的数据是utf8格式的
pipeline.py中file = codecs.open('/%s.txt' % (item['name']), mode = 'w',encoding='utf-8')将数据以utf8格式存储
相关文章推荐
- 关于调试过程中单片机复位的几点心得与体会
- 关于调试过程中单片机复位的几点心得与体会
- SQL在存储过程设计心得体会和优化措施
- 近期学习Lua、C、 Erlang过程中的心得体会
- Python.Scrapy安装过程中的心得
- 大牛的调试程序心得体会
- "诗词大闯关"调查过程心得体会
- 分页存储过程心得体会
- 项目改造过程与心得体会-1-架构
- mini2440+jlinkv8+mdk4.54 sony3.5寸tft lcd 调试心得体会
- mini2440+jlink v8+mkd4.54 uart串口通信调试心得体会(11.20补完中断部分)
- 这是我在学习过程中的心得体会,希望自己变得更强
- Mongo的Replica Sets (复制集)的配置全过程和心得体会
- jrtplib3.7.1编译调试心得体会
- Mongo的Replica Sets (复制集)的配置全过程和心得体会
- 来记录research过程中的心得,体会,还有相关学习资料
- IAR5.4+JLINK RDI调试程序心得体会
- 调试kernel时对bios加电后过程的一些心得
- Mongo的Replica Sets (复制集)的配置全过程和心得体会
- C# Web开发学习----记录学习过程与心得体会