Scrapy爬虫笔记-未完成

本文介绍了如何启动Scrapy爬虫,包括命令行方式和API在脚本中的使用。讨论了XPath定位技巧,并提到了Firefox和Chrome的辅助工具。此外,文章还涉及了登录爬取的策略、随机User-agent的设置以及通过下载器中间件实现。最后,讲解了如何将数据存储到MySQL数据库,包括SQLStorePipeline的使用。
摘要由CSDN通过智能技术生成
  1. 启动Scrapy爬虫
    除了常用的 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)

  5. 关于数据库存储(以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)
  1. 在脚本中运行Scrapy
    http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/practices.html#run-from-script
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值