Scrapy
  • 初窥Scrapy
    • 选择一个网站
    • 定义您想抓取的数据
    • 编写提取数据的Spider
    • 执行spider,获取数据
    • 查看提取到的数据
    • 还有什么?
    • 接下来
  • 安装指南
    • 安装Scrapy
    • 平台安装指南
  • Scrapy入门教程
    • 创建项目
    • 定义Item
    • 编写第一个爬虫(Spider)
    • 保存爬取到的数据
    • 下一步
  • 例子
  • 命令行工具(Command line tools)
    • 默认的Scrapy项目结构
    • 使用 scrapy 工具
    • 可用的工具命令(tool commands)
    • 自定义项目命令
  • Items
    • 声明Item
    • Item字段(Item Fields)
    • 与Item配合
    • 扩展Item
    • Item对象
    • 字段(Field)对象
  • Spiders
    • Spider参数
    • 内置Spider参考手册
  • 选择器(Selectors)
    • 使用选择器(selectors)
    • 内建选择器的参考
  • Item Loaders
    • Using Item Loaders to populate items
    • Input and Output processors
    • Declaring Item Loaders
    • Declaring Input and Output Processors
    • Item Loader Context
    • ItemLoader objects
    • Reusing and extending Item Loaders
    • Available built-in processors
  • Scrapy终端(Scrapy shell)
    • 启动终端
    • 使用终端
    • 终端会话(shell session)样例
    • 在spider中启动shell来查看response
  • Item Pipeline
    • 编写你自己的item pipeline
    • Item pipeline 样例
    • 启用一个Item Pipeline组件
  • Feed exports
    • 序列化方式(Serialization formats)
    • 存储(Storages)
    • 存储URI参数
    • 存储端(Storage backends)
    • 设定(Settings)
  • Link Extractors
    • 内置Link Extractor 参考
  • Logging
    • Log levels
    • 如何设置log级别
    • 如何记录信息(log messages)
    • 在Spider中添加log(Logging from Spiders)
    • scrapy.log模块
    • Logging设置
  • 数据收集(Stats Collection)
    • 常见数据收集器使用方法
    • 可用的数据收集器
  • 发送email
    • 简单例子
    • MailSender类参考手册
    • Mail设置
  • Telnet终端(Telnet Console)
    • 如何访问telnet终端
    • telnet终端中可用的变量
    • Telnet console usage examples
    • Telnet终端信号
    • Telnet设定
  • Web Service
  • 常见问题(FAQ)
    • Scrapy相BeautifulSoup或lxml比较,如何呢?
    • Scrapy支持那些Python版本?
    • Scrapy支持Python 3么?
    • Scrapy是否从Django中”剽窃”了X呢?
    • Scrapy支持HTTP代理么?
    • 如何爬取属性在不同页面的item呢?
    • Scrapy退出,ImportError: Nomodule named win32api
    • 我要如何在spider里模拟用户登录呢?
    • Scrapy是以广度优先还是深度优先进行爬取的呢?
    • 我的Scrapy爬虫有内存泄露,怎么办?
    • 如何让Scrapy减少内存消耗?
    • 我能在spider中使用基本HTTP认证么?
    • 为什么Scrapy下载了英文的页面,而不是我的本国语言?
    • 我能在哪里找到Scrapy项目的例子?
    • 我能在不创建Scrapy项目的情况下运行一个爬虫(spider)么?
    • 我收到了 “Filtered offsite request” 消息。如何修复?
    • 发布Scrapy爬虫到生产环境的推荐方式?
    • 我能对大数据(large exports)使用JSON么?
    • 我能在信号处理器(signal handler)中返回(Twisted)引用么?
    • reponse返回的状态值999代表了什么?
    • 我能在spider中调用 pdb.set_trace() 来调试么?
    • 将所有爬取到的item转存(dump)到JSON/CSV/XML文件的最简单的方法?
    • 在某些表单中巨大神秘的 __VIEWSTATE 参数是什么?
    • 分析大XML/CSV数据源的最好方法是?
    • Scrapy自动管理cookies么?
    • 如何才能看到Scrapy发出及接收到的Cookies呢?
    • 要怎么停止爬虫呢?
    • 如何避免我的Scrapy机器人(bot)被禁止(ban)呢?
    • 我应该使用spider参数(arguments)还是设置(settings)来配置spider呢?
    • 我爬取了一个XML文档但是XPath选择器不返回任何的item
  • 调试(Debugging)Spiders
    • Parse命令
    • Scrapy终端(Shell)
    • 在浏览器中打开
    • Logging
  • Spiders Contracts
    • 自定义Contracts
  • 实践经验(Common Practices)
    • 在脚本中运行Scrapy
    • 同一进程运行多个spider
    • 分布式爬虫(Distributed crawls)
    • 避免被禁止(ban)
    • 动态创建Item类
  • 通用爬虫(Broad Crawls)
    • 增加并发
    • 降低log级别
    • 禁止cookies
    • 禁止重试
    • 减小下载超时
    • 禁止重定向
    • 启用 “Ajax Crawlable Pages” 爬取
  • 借助Firefox来爬取
    • 在浏览器中检查DOM的注意事项
    • 对爬取有帮助的实用Firefox插件
  • 使用Firebug进行爬取
    • 介绍
    • 获取到跟进(follow)的链接
    • 提取数据
  • 调试内存溢出
    • 内存泄露的常见原因
    • 使用 trackref 调试内存泄露
    • 使用Guppy调试内存泄露
    • Leaks without leaks
  • 下载项目图片
    • 使用图片管道
    • 使用样例
    • 开启你的图片管道
    • 图片存储
    • 额外的特性
    • 实现定制图片管道
    • 定制图片管道的例子
  • Ubuntu 软件包
  • Scrapyd
  • 自动限速(AutoThrottle)扩展
    • 设计目标
    • 扩展是如何实现的
    • 限速算法
    • 设置
  • Benchmarking
  • Jobs: 暂停,恢复爬虫
    • Job 路径
    • 怎么使用
    • 保持状态
    • 持久化的一些坑
  • DjangoItem
    • 使用DjangoItem
    • DjangoItem注意事项
    • 配置Django的设置
  • 架构概览
    • 概述
    • 组件
    • 数据流(Data flow)
    • 事件驱动网络(Event-driven networking)
  • 下载器中间件(Downloader Middleware)
    • 激活下载器中间件
    • 编写您自己的下载器中间件
    • 内置下载中间件参考手册
  • Spider中间件(Middleware)
    • 激活spider中间件
    • 编写您自己的spider中间件
    • 内置spider中间件参考手册
  • 扩展(Extensions)
    • 扩展设置(Extension settings)
    • 加载和激活扩展
    • 可用的(Available)、开启的(enabled)和禁用的(disabled)的扩展
    • 禁用扩展(Disabling an extension)
    • 实现你的扩展
    • 内置扩展介绍
  • 核心API
    • Crawler API
    • 设置(Settings) API
    • SpiderManager API
    • 信号(Signals) API
    • 状态收集器(Stats Collector) API
  • Requests and Responses
    • Request对象
    • Request.meta special keys
    • Request subclasses
    • Response objects
    • Response subclasses
  • Settings
    • 指定设定(Designating the settings)
    • 获取设定值(Populating the settings)
    • 如何访问设定(How to access settings)
    • 设定名字的命名规则
    • 内置设定参考手册
  • 信号(Signals)
    • 延迟的信号处理器(Deferred signal handlers)
    • 内置信号参考手册(Built-in signals reference)
  • 异常(Exceptions)
    • 内置异常参考手册(Built-in Exceptions reference)
  • Item Exporters
    • 使用 Item Exporter
    • Item Exporters 参考资料
  • Release notes
    • 0.24.4 (2014-08-09)
    • 0.24.3 (2014-08-09)
    • 0.24.2 (2014-07-08)
    • 0.24.1 (2014-06-27)
    • 0.24.0 (2014-06-26)
    • 0.22.2 (released 2014-02-14)
    • 0.22.1 (released 2014-02-08)
    • 0.22.0 (released 2014-01-17)
    • 0.20.2 (released 2013-12-09)
    • 0.20.1 (released 2013-11-28)
    • 0.20.0 (released 2013-11-08)
    • 0.18.4 (released 2013-10-10)
    • 0.18.3 (released 2013-10-03)
    • 0.18.2 (released 2013-09-03)
    • 0.18.1 (released 2013-08-27)
    • 0.18.0 (released 2013-08-09)
    • 0.16.5 (released 2013-05-30)
    • 0.16.4 (released 2013-01-23)
    • 0.16.3 (released 2012-12-07)
    • 0.16.2 (released 2012-11-09)
    • 0.16.1 (released 2012-10-26)
    • 0.16.0 (released 2012-10-18)
    • 0.14.4
    • 0.14.3
    • 0.14.2
    • 0.14.1
    • 0.14
    • 0.12
    • 0.10
    • 0.9
    • 0.8
    • 0.7
  • Contributing to Scrapy
    • Reporting bugs
    • Writing patches
    • Submitting patches
    • Coding style
    • Scrapy Contrib
    • Documentation policies
    • Tests
  • Versioning and API Stability
    • Versioning
    • API Stability
  • 试验阶段特性
    • 使用外部库插入命令
 
Scrapy
  • Docs »
  • Link Extractors
  • 返回在线手册首页

Link Extractors¶

Link Extractors 是用于从网页(scrapy.http.Response )中抽取会被follow的链接的对象。

Scrapy默认提供2种可用的 Link Extractor, 但你通过实现一个简单的接口创建自己定制的Link Extractor来满足需求。 Scrapy 提供了 scrapy.contrib.linkextractors import LinkExtractor , 不过您也可以通过实现一个简单的接口来创建您自己的Link Extractor,满足需求。

每个LinkExtractor有唯一的公共方法是 extract_links ,其接收 一个 Response 对象, 并返回 scrapy.link.Link 对象。 Link Extractors只实例化一次,其 extract_links 方法会根据不同的response被调用多次来提取链接。

Link Extractors在 CrawlSpider 类(在Scrapy可用)中使用, 通过一套规则,但你也可以用它在你的Spider中,即使你不是从 CrawlSpider 继承的子类, 因为它的目的很简单: 提取链接。

内置Link Extractor 参考¶

Scrapy 自带的Link Extractors类在 scrapy.contrib.linkextractors 模块提供。

默认的link extractor 是 LinkExtractor ,其实就是 LxmlLinkExtractor:

from scrapy.contrib.linkextractors import LinkExtractor

在以前版本的Scrapy版本中提供了其他的link extractor,不过都已经被废弃了。

LxmlLinkExtractor¶

class scrapy.contrib.linkextractors.lxmlhtml.LxmlLinkExtractor(allow=(), deny=(), allow_domains=(), deny_domains=(), deny_extensions=None, restrict_xpaths=(), tags=('a', 'area'), attrs=('href', ), canonicalize=True, unique=True, process_value=None)¶

LxmlLinkExtractor is the recommended link extractor with handy filtering options. It is implemented using lxml’s robust HTMLParser.

参数:
  • allow (a regular expression (or list of)) – a single regular expression (or list of regular expressions) that the (absolute) urls must match in order to be extracted. If not given (or empty), it will match all links.
  • deny (a regular expression (or list of)) – a single regular expression (or list of regular expressions) that the (absolute) urls must match in order to be excluded (ie. not extracted). It has precedence over the allow parameter. If not given (or empty) it won’t exclude any links.
  • allow_domains (str or list) – a single value or a list of string containing domains which will be considered for extracting the links
  • deny_domains (str or list) – a single value or a list of strings containing domains which won’t be considered for extracting the links
  • deny_extensions (list) – a single value or list of strings containing extensions that should be ignored when extracting links. If not given, it will default to the IGNORED_EXTENSIONS list defined in the scrapy.linkextractor module.
  • restrict_xpaths (str or list) – is a XPath (or list of XPath’s) which defines regions inside the response where links should be extracted from. If given, only the text selected by those XPath will be scanned for links. See examples below.
  • tags (str or list) – a tag or a list of tags to consider when extracting links. Defaults to ('a', 'area').
  • attrs (list) – an attribute or list of attributes which should be considered when looking for links to extract (only for those tags specified in the tags parameter). Defaults to ('href',)
  • canonicalize (boolean) – canonicalize each extracted url (using scrapy.utils.url.canonicalize_url). Defaults to True.
  • unique (boolean) – whether duplicate filtering should be applied to extracted links.
  • process_value (callable) –

    它接收来自扫描标签和属性提取每个值, 可以修改该值, 并返回一个新的, 或返回 None 完全忽略链接的功能。如果没有给出, process_value 默认是 lambda x: x。

    例如,从这段代码中提取链接:

    <a href="javascript:goToPage('../other/page.html'); return false">Link text</a>
    

    你可以使用下面的这个 process_value 函数:

    def process_value(value):
        m = re.search("javascript:goToPage\('(.*?)'", value)
        if m:
            return m.group(1)
    

Next Previous

© 版权所有 2008-2014, written by Scrapy developers, translated by Summer&Friends. 最后更新于 Dec 04, 2014.