Scrapy爬虫笔记-未完成
2016-02-01 11:01
393 查看
启动Scrapy爬虫
除了常用的 scrapy crawl 来启动Scrapy,您也可以使用 API 在脚本中启动Scrapy。
XPath 定位
Firebug(Firefox插件)
可以使用Chrome的XPath helper
firefox上的若干插件
关于登陆爬取
http://outofmemory.cn/code-snippet/16528/scrapy-again-to-code
随机User-agent
设置下载器中间件(DownloadMiddleWare)
关于数据库存储(以MySQL为例)
import datetime
import MySQLdb.cursors
class SQLStorePipeline(object):
在脚本中运行Scrapy
http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/practices.html#run-from-script
除了常用的 scrapy crawl 来启动Scrapy,您也可以使用 API 在脚本中启动Scrapy。
XPath 定位
Firebug(Firefox插件)
可以使用Chrome的XPath helper
firefox上的若干插件
关于登陆爬取
http://outofmemory.cn/code-snippet/16528/scrapy-again-to-code
随机User-agent
设置下载器中间件(DownloadMiddleWare)
关于数据库存储(以MySQL为例)
Cannot use this to create the table, must have table already created
from twisted.enterprise import adbapiimport 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)
在脚本中运行Scrapy
http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/practices.html#run-from-script
相关文章推荐
- spirngmvc整合mybatis实现CRUD
- 利用jspx解决jsp后缀被限制拿shell
- impdp ORA-29913: error in executing ODCIEXTTABLEOPEN callout
- poj1284
- poj2407
- 利用LoadRunner编写socket性能测试脚本
- 金融信息交换协议(FIX)v5.0读书笔记(0)
- 更新ADT到Android L的方法
- QT5的软键盘输入法实现
- mac系统如何显示和隐藏文件
- 锁
- 进度条的写法
- 博客园添加访问人数统计
- commonJS — 浏览器操作(for Browser)
- zz一个失败的复旦学生的奋斗生涯
- Android中SwipeBack实现右滑返回效果
- 移植opencv到龙芯1c上
- js数组和字符串去重复几种方法
- 【Linux学习009】脚本编程之变量、条件测试和条件判断
- python中的实例方法、静态方法、类方法、类变量和实例变量浅析