Python爬虫框架Scrapy 学习笔记 5 ------- 使用pipelines过滤敏感词

还是上一篇博客的那个网站,我们增加了pipeline.py

items.py

from scrapy.item import Item, Field

class Website(Item):

    name = Field()
    description = Field()
    url = Field()

dmoz.py

from scrapy.spider import Spider
from scrapy.selector import Selector

from dirbot.items import Website

class DmozSpider(Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/",
    ]

    def parse(self, response):
        """
        The lines below is a spider contract. For more info see:
        http://doc.scrapy.org/en/latest/topics/contracts.html

        @url http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/
        @scrapes name
        """
        sel = Selector(response)
        sites = sel.xpath(‘//ul[@class="directory-url"]/li‘)
        items = []

        for site in sites:
            item = Website()
            item[‘name‘] = site.xpath(‘a/text()‘).extract()
            item[‘url‘] = site.xpath(‘a/@href‘).extract()
            item[‘description‘] = site.xpath(‘text()‘).re(‘-\s[^\n]*\\r‘)
            items.append(item)

        return items

注意description的xpath与上一次有所不同,这里删除了空格和换行

pipeline.py

from scrapy.exceptions import DropItem

class FilterWordsPipeline(object):
    """A pipeline for filtering out items which contain certain words in their
    description"""

    # put all words in lowercase
    words_to_filter = [‘politics‘, ‘religion‘]

    def process_item(self, item, spider):
        for word in self.words_to_filter:
            if word in unicode(item[‘description‘]).lower():
                raise DropItem("Contains forbidden word: %s" % word)
        else:
            return item

作用:过滤description中包含 ‘politics‘或‘religion‘的item

时间: 2024-08-27 16:38:23

Python爬虫框架Scrapy 学习笔记 5 ------- 使用pipelines过滤敏感词的相关文章

Python爬虫框架Scrapy 学习笔记 8----Spider

什么是爬虫? 从逻辑角度讲,爬虫对应一个树.树枝是网页,树叶是感兴趣的信息. 当我们从一个URL出发查找感兴趣的信息时,当前URL返回的内容可能包含我们感兴趣的信息,也可能包含另一个可能包含我们感兴趣的信息的URL.一个爬虫对应一次信息搜索,信息搜索过程会建立起一棵树. scrapy.Spider这个类提供了接口,允许我们设计整个信息搜索的流程. 给spider传递运行时所需的参数.比如URL ?号后面的参数信息.这些信息可以选择使用crawl -a 命令传递 2. Spider循环 对spid

Python爬虫框架Scrapy 学习笔记 7------- scrapy.Item源码剖析

在前面的example中,我们知道定义一个Item类很简单,只要继承scrapy.Item,然后添加几个类型为scrapy.Field的对象作为类属性,就像下面这样 import scrapy class Product(scrapy.Item):     name = scrapy.Field()     price = scrapy.Field()     stock = scrapy.Field()     last_updated = scrapy.Field(serializer=st

Python爬虫框架Scrapy 学习笔记 10.3 -------【实战】 抓取天猫某网店所有宝贝详情

第三部分 替换默认下载器,使用selenium下载页面 对详情页稍加分析就可以得出:我们感兴趣的大部分信息都是由javascript动态生成的,因此需要先在浏览器中执行javascript代码,再从最终的页面上抓取信息(当然也有别的解决方案). scrapy 本身提供了 Middleware机制.利用Dowloader Middleware可以改变默认的request对象和reponse对象,甚至替换默认的下载器.(可以对比JavaEE中的Filter理解Middleware) scrapy 架

Python爬虫框架Scrapy 学习笔记 1 ----- 安装

一. 安装 平台 windows 7 1. 安装 python2.7 32 位 2. 安装 Python2.7-twisted-14.0.2  下载msi安装包双击安装即可 3. 安装 python2.7对应的pip 4. 配置好python的环境变量后,打开cmd运行 : pip install scrapy pip默认为我安装了 Scrapy 0.24.4 二.相关文档下载 官方提供了PDF格式的文档,点击下载即可.文档只有205页.

Python爬虫框架Scrapy 学习笔记 4 ------- 第二个Scrapy项目

1. 任务一,抓取以下两个URL的内容,写入文件 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/ http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/ 项目截图 和上一个project不同的是,在spider中没有定义rules属性,而是定义了parse方法.这个方法告诉scrapy抓取start urls的内容后应该怎么做.第

Python爬虫框架Scrapy 学习笔记 10.2 -------【实战】 抓取天猫某网店所有宝贝详情

第二部分 抽取起始页中进入宝贝详情页面的链接 创建项目,并生成spider模板,这里使用crawlspider. 2. 在中scrapy shell中测试选取链接要使用的正则表达式. 首先使用firefox和firebug查看源码,定位到要链接 然后在shell中打开网页: scrapy shell  http://shanhuijj.tmall.com/search.htm?spm=a1z10.3-b.w4011-3112923129.1.KjGkS2 确定正则表达式为:http://deta

Python爬虫框架Scrapy 学习笔记 9 ----selenium

selenium本是用来对web application做自动化测试的.不过,它有个天大的好处:能让我们用python(当然不仅是python)代码模拟人对浏览器的操作. 所需软件:python2.7 , firefox 25.0.1(版本不能太高), selenium2.44.0(使用pip install selenium安装即可) 1. 打开浏览器,请求百度主页,5秒钟后关闭浏览器 from selenium import webdriver import  time brower = w

Python爬虫框架Scrapy 学习笔记 6 ------- 基本命令

1. 有些scrapy命令,只有在scrapy project根目录下才available,比如crawl命令 2 . scrapy genspider taobao http://detail.tmall.com/item.htm?id=12577759834 自动在spider目录下生成taobao.py # -*- coding: utf-8 -*- import scrapy class TaobaoSpider(scrapy.Spider):     name = "taobao&qu

Python爬虫框架Scrapy 学习笔记 3 ------- 第一个Scrapy项目

开发环境PyCharm 目标网站和上一次一样,可参考:http://dingbo.blog.51cto.com/8808323/1597695 但是这次不是在单个文件中运行,而是创建一个scrapy项目 1.使用命令行工具创建scrapy项目的基本目录结构 2. 编辑items.py 3.在spiders目录下,新建spider1.py 报错很正常 我们按照scrapy project的目录结构导入了TorrentItem,而不是pyCharm project的目录结构 4.运行spider 抓