调试Scrapy过程中的心得体会
1.大量抓取网页时出现“Memory Error”解决办法:设置一个队列,每当爬虫空闲时才向队列中放入请求,例如:
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格式存储
调试Scrapy过程中的心得体会的更多相关文章
- xp硬盘安装Fedora14 过程记录及心得体会(fedora14 live版本680M 和fedora14 DVD版本3.2G的选择)
这次电脑奔溃了,奇怪的是直接ghost覆盖c盘竟然不中.之前电脑上硬盘安装的fedora14操作系统,也是双系统.不知道是不是这个问题,记得同学说过,在硬盘装fedora之后,要手动修改c盘隐藏的那个 ...
- 疑问:VS在调试的过程中,总是会提示正在加载picface.dll的符号,然后卡死在那
环境: 硬件环境: PC 软件环境: Windows7 VS2012 MFC程序调试 现象: 调试的过程中,提示“正在从以下目录加载picface.dll的符号:C:\windows\dll”,然后就 ...
- 使用Android Studio调试UiAutomator过程中遇到的问题
声明: 这里纪录了个人学习和使用Android Studio调试UiAutomator过程中遇到遇到的问题,不定时进行更新,欢迎一起交流学习 1.Excution faild for task ‘:a ...
- Python安装scrapy过程中出现“Failed building wheel for xxx”
https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml Python安装scrapy库过程中出现“ Failed building wheel for xxx ...
- 安装Scrapy过程中遇到的几个问题总结
安装Scrapy 1.https://www.lfd.uci.edu/~gohlke/pythonlibs/下载 Twisted 安装 Twisted-19.10.0-cp37-cp37m-win_a ...
- 获取图书isbn信息 共享图书开发 图书信息接口开发过程中的心得体会
最近做一个图书共享的项目,需要用户扫一扫书籍后面的一维码,获取到书籍的isbn号码,然后通过这个isbn号码能够直接获取到这本书的名字.简介.价格.图片等信息. 于是百度搜了下,之前很多的豆瓣的接口, ...
- vs2012 在调试或运行的过程中不能加断点
在使用VS2012 的过程中,突然发现在调试的过程中,不能加断点,显示断点未能绑定.在搜寻了很多解决方案后未能解决,3.23这一天,重装了VS也没有用. 便想着把网上所有的方法都试个遍也要解决这个问题 ...
- Android APP 调试过程中遇到的问题。
调试过过程中APP安装完启动后有的时候会异常退出,报这个错误.有的时候可以直接启动.查找不到原因.网上说把commit方法替换成commitAllowingStateLoss() 也无效. Andro ...
- 百度自动发贴,登录很顺利的模拟实现,但发贴攻关失败,能力有限,追JS过程中颇为痛苦
攻关失败,且短期内看不到希望,看不到方向,且越来越焦急,目前已知的是,用根据用户的鼠标事件以一定的规则结合其他数据,服务器以这些数据验证是否为真正的手动发贴. 不过闲暇时实现了百度贴吧的自动签到. 较 ...
随机推荐
- hash function 字符串哈希函数
#include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...
- Visual Studio 试用版的评估期已经结束【解决方案】
原文发布时间为:2010-01-09 -- 来源于本人的百度文章 [由搬家工具导入] Visual Studio 试用版的评估期已经结束…… 解决。2010年1月9日0:02:50,本人亲自尝试,可行 ...
- Android开发跳坑记录
本文主要记录在Android开发中遇见的一些问题,以及解决方法. 2015.12.01 1.adb.exe 端口被占用 解决: http://blog.csdn.net/xiaanming/artic ...
- 利用input-radio和input-checkbox的表单特性可以节省很多js代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- android程序入口
android程序的真正入口是Application类的onCreate方法
- activeMQ队列模式和主题模式的Java实现
一.队列模式 生产者 import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destina ...
- LeetCode OJ-- Count and Say
https://oj.leetcode.com/problems/count-and-say/ 求经过n次变换后,变成了什么. 1 11 21 1211 111221 ps. 3 变成 ‘3 ...
- Codeforces 629 B. Far Relative’s Problem
B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- UVA 10976 Fractions Again?!【暴力枚举/注意推导下/分子分母分开保存】
[题意]:给你一个数k,求所有使得1/k = 1/x + 1/y成立的x≥y的整数对. [分析]:枚举所有在区间[k+1, 2k]上的 y 即可,当 1/k - 1/y 的结果分子为1即为一组解. [ ...
- 关于js延迟加载(异步操作)的方式
一.概述 最近重新开始学习js,在第一章的一个小节里写到了“脚本调用策略”,书上写的这部分不多,但是发现在我之前的(笔)面试中,问到的频率还是比较高的.自己一直习惯于直接把所有js文件写在head里, ...