• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 基于scrapy实现的简单蜘蛛采集程序

基于scrapy实现的简单蜘蛛采集程序

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了scrapy,scrapy安装,scrapy教程,scrapy中文文档,python scrapy等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了基于scrapy实现的简单蜘蛛采集程序。分享给大家供大家参考。具体如下:

# Standard Python library imports
# 3rd party imports
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
# My imports
from poetry_analysis.items import PoetryAnalysisItem
HTML_FILE_NAME = r'.+\.html'
class PoetryParser(object):
  """
  Provides common parsing method for poems formatted this one specific way.
  """
  date_pattern = r'(\d{2} \w{3,9} \d{4})'
 
  def parse_poem(self, response):
    hxs = HtmlXPathSelector(response)
    item = PoetryAnalysisItem()
    # All poetry text is in pre tags
    text = hxs.select('//pre/text()').extract()
    item['text'] = ''.join(text)
    item['url'] = response.url
    # head/title contains title - a poem by author
    title_text = hxs.select('//head/title/text()').extract()[0]
    item['title'], item['author'] = title_text.split(' - ')
    item['author'] = item['author'].replace('a poem by', '')
    for key in ['title', 'author']:
      item[key] = item[key].strip()
    item['date'] = hxs.select("//p[@class='small']/text()").re(date_pattern)
    return item
class PoetrySpider(CrawlSpider, PoetryParser):
  name = 'example.com_poetry'
  allowed_domains = ['www.example.com']
  root_path = 'someuser/poetry/'
  start_urls = ['http://www.example.com/someuser/poetry/recent/',
         'http://www.example.com/someuser/poetry/less_recent/']
  rules = [Rule(SgmlLinkExtractor(allow=[start_urls[0] + HTML_FILE_NAME]),
                  callback='parse_poem'),
       Rule(SgmlLinkExtractor(allow=[start_urls[1] + HTML_FILE_NAME]),
                  callback='parse_poem')]
</div>

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

</div>

您可能想查找下面的文章:

  • 使用Python的Scrapy框架十分钟爬取美女图
  • 使用Python的Scrapy框架十分钟爬取美女图
  • Python中__init__.py文件的作用详解
  • Python中__init__.py文件的作用详解
  • Python抓取框架 Scrapy的架构
  • Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
  • Python的爬虫程序编写框架Scrapy入门学习教程
  • Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
  • Python的爬虫程序编写框架Scrapy入门学习教程
  • Python模块包中__init__.py文件功能分析

相关文章

  • Python实现二维有序数组查找的方法
  • python连接mysql并提交mysql事务示例
  • 用python + openpyxl处理excel2007文档思路以及心得
  • python动态性强类型用法实例
  • Python常用模块用法分析
  • python使用os模块的os.walk遍历文件夹示例
  • 详细解读Python的web.py框架下的application.py模块
  • python 统计代码行数简单实例
  • 使用基于Python的Tornado框架的HTTP客户端的教程
  • python中类变量与成员变量的使用注意点总结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 详解Python的Django框架中的templates设置
    • 在Python中使用SQLite的简单教程
    • 用Python编写一个简单的Lisp解释器的教程
    • Python中操作符重载用法分析
    • python3中int(整型)的使用教程
    • python递归删除指定目录及其所有内容的方法
    • python Django模板的使用方法(图文)
    • python动态加载变量示例分享
    • python一键升级所有pip package的方法
    • 解读Django框架中的低层次缓存API

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有