Scrapy爬虫框架笔记
2016-02-04 09:46
183 查看
1. 启动Scrapy爬虫
除了常用的
2. XPath 定位
Firebug(Firefox插件)
可以使用Chrome的XPath helper
firefox上的若干插件
3. 关于登陆爬取
http://outofmemory.cn/code-snippet/16528/scrapy-again-to-code
4. 随机User-agent
设置下载器中间件(DownloadMiddleWare)
4. 关于数据库存储(以MySQL为例)
5. 在脚本中运行Scrapy
http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/practices.html#run-from-script
除了常用的
scrapy crawl来启动Scrapy,您也可以使用 API 在脚本中启动Scrapy。
2. XPath 定位
Firebug(Firefox插件)
可以使用Chrome的XPath helper
firefox上的若干插件
3. 关于登陆爬取
http://outofmemory.cn/code-snippet/16528/scrapy-again-to-code
4. 随机User-agent
设置下载器中间件(DownloadMiddleWare)
4. 关于数据库存储(以MySQL为例)
# Cannot use this to create the table, must have table already created from twisted.enterprise import adbapi import datetime import MySQLdb.cursors class SQLStorePipeline(object): def __init__(self): self.dbpool = adbapi.ConnectionPool('MySQLdb', db='mydb', user='myuser', passwd='mypass', cursorclass=MySQLdb.cursors.DictCursor, charset='utf8', use_unicode=True) def process_item(self, item, spider): # run db query in thread pool query = self.dbpool.runInteraction(self._conditional_insert, item) query.addErrback(self.handle_error) return item def _conditional_insert(self, tx, item): # create record if doesn't exist. # all this block run on it's own thread tx.execute("select * from websites where link = %s", (item['link'][0], )) result = tx.fetchone() if result: log.msg("Item already stored in db: %s" % item, level=log.DEBUG) else: tx.execute(\ "insert into websites (link, created) " "values (%s, %s)", (item['link'][0], datetime.datetime.now()) ) log.msg("Item stored in db: %s" % item, level=log.DEBUG) def handle_error(self, e): log.err(e)
5. 在脚本中运行Scrapy
http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/practices.html#run-from-script
相关文章推荐
- SesJs
- 分享我对JS插件开发的一些感想和心得
- sqlserver 企业版下载地址
- 数据结构-散列表
- Fragment嵌套ViewPager切换后数据消失ViewPager空白问题
- [javascript-snippet]使用javascript+html5实现图片的灰度处理
- 查看图片翻转功能
- Spark Streaming学习与实践(2)
- APP数据共享
- 转换坐标系
- SQL Server 诊断查询-(5)
- centos 5.6.2上不了网
- sql server 服务作用简介
- UILabel的混合显示动画效果
- 函数定义、函数属性和方法
- SQL Server 诊断查询-(4)
- Mysql coalesce()函数认识和用法
- MySQL中事务的概述ACID了解
- Python-一张图
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码