Python自定义scrapy中间模块避免重复采集的方法


Posted in Python onApril 07, 2015

本文实例讲述了Python自定义scrapy中间模块避免重复采集的方法。分享给大家供大家参考。具体如下:

from scrapy import log
from scrapy.http import Request
from scrapy.item import BaseItem
from scrapy.utils.request import request_fingerprint
from myproject.items import MyItem
class IgnoreVisitedItems(object):
  """Middleware to ignore re-visiting item pages if they
  were already visited before. 
  The requests to be filtered by have a meta['filter_visited']
  flag enabled and optionally define an id to use 
  for identifying them, which defaults the request fingerprint,
  although you'd want to use the item id,
  if you already have it beforehand to make it more robust.
  """
  FILTER_VISITED = 'filter_visited'
  VISITED_ID = 'visited_id'
  CONTEXT_KEY = 'visited_ids'
  def process_spider_output(self, response, result, spider):
    context = getattr(spider, 'context', {})
    visited_ids = context.setdefault(self.CONTEXT_KEY, {})
    ret = []
    for x in result:
      visited = False
      if isinstance(x, Request):
        if self.FILTER_VISITED in x.meta:
          visit_id = self._visited_id(x)
          if visit_id in visited_ids:
            log.msg("Ignoring already visited: %s" % x.url,
                level=log.INFO, spider=spider)
            visited = True
      elif isinstance(x, BaseItem):
        visit_id = self._visited_id(response.request)
        if visit_id:
          visited_ids[visit_id] = True
          x['visit_id'] = visit_id
          x['visit_status'] = 'new'
      if visited:
        ret.append(MyItem(visit_id=visit_id, visit_status='old'))
      else:
        ret.append(x)
    return ret
  def _visited_id(self, request):
    return request.meta.get(self.VISITED_ID) or request_fingerprint(request)

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python 文件和路径操作函数小结
Nov 23 Python
SublimeText 2编译python出错的解决方法(The system cannot find the file specified)
Nov 27 Python
举例区分Python中的浅复制与深复制
Jul 02 Python
Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
Jun 14 Python
Python2/3中urllib库的一些常见用法
Dec 19 Python
数组保存为txt, npy, csv 文件, 数组遍历enumerate的方法
Jul 09 Python
python 通过麦克风录音 生成wav文件的方法
Jan 09 Python
Python @property使用方法解析
Sep 17 Python
Python 函数用法简单示例【定义、参数、返回值、函数嵌套】
Sep 20 Python
pandas数据处理进阶详解
Oct 11 Python
python实现获取单向链表倒数第k个结点的值示例
Oct 24 Python
keras 获取某层输出 获取复用层的多次输出实例
May 23 Python
Python中用memcached来减少数据库查询次数的教程
Apr 07 #Python
Python3中常用的处理时间和实现定时任务的方法的介绍
Apr 07 #Python
Python中使用pprint函数进行格式化输出的教程
Apr 07 #Python
利用QT写一个极简单的图形化Python闹钟程序
Apr 07 #Python
分析Python编程时利用wxPython来支持多线程的方法
Apr 07 #Python
Python中尝试多线程编程的一个简明例子
Apr 07 #Python
Python的Flask框架中Flask-Admin库的简单入门指引
Apr 07 #Python
You might like
基于在生产环境中使用php性能测试工具xhprof的详解
2013/06/03 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
2014/08/05 PHP
yii中widget的用法
2014/12/03 PHP
php使用数组填充下拉列表框的方法
2015/03/31 PHP
ThinkPHP文件缓存类代码分享
2015/04/22 PHP
php实现URL加密解密的方法
2016/11/17 PHP
js资料toString 方法
2007/03/13 Javascript
十个迅速提升JQuery性能让你的JQuery跑得更快
2012/12/10 Javascript
javascript操作css属性
2013/12/30 Javascript
jQuery学习笔记之jQuery构建函数的7种方法
2014/06/03 Javascript
JQuery中使用Ajax赋值给全局变量失败异常的解决方法
2014/08/18 Javascript
JavaScript中九种常用排序算法
2014/09/02 Javascript
js实现同一页面可多次调用的图片幻灯切换效果
2015/02/28 Javascript
jQuery插件开发汇总
2016/05/15 Javascript
使用vue构建一个上传图片表单
2017/07/04 Javascript
iview中Select 选择器多选校验方法
2018/03/15 Javascript
Vue+axios+WebApi+NPOI导出Excel文件实例方法
2019/06/05 Javascript
解决vue v-for src 图片路径问题 404
2019/11/12 Javascript
[01:04:22]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 IG vs EG
2018/04/01 DOTA
[09:59]DOTA2-DPC中国联赛2月7日Recap集锦
2021/03/11 DOTA
Python中实现两个字典(dict)合并的方法
2014/09/23 Python
利用Python对文件夹下图片数据进行批量改名的代码实例
2019/02/21 Python
python中import与from方法总结(推荐)
2019/03/21 Python
Python递归及尾递归优化操作实例分析
2020/02/01 Python
Python3查找列表中重复元素的个数的3种方法详解
2020/02/13 Python
Python过滤序列元素的方法
2020/07/31 Python
PyQt5的QWebEngineView使用示例
2020/10/20 Python
美国珠宝网上商店:Jeulia
2016/09/01 全球购物
草莓网官网:StrawberryNET
2019/08/21 全球购物
项目资料员岗位职责
2013/12/10 职场文书
大家检讨书5000字
2014/02/03 职场文书
经典商业广告词
2014/03/13 职场文书
公共艺术专业自荐信
2014/09/01 职场文书
法定授权委托证明书
2015/06/18 职场文书
2019秋季运动会口号
2019/06/25 职场文书
《自然之道》读后感3篇
2019/12/17 职场文书