scrapy spider的几种爬取方式实例代码


Posted in Python onJanuary 25, 2018

本节课介绍了scrapy的爬虫框架,重点说了scrapy组件spider。

spider的几种爬取方式:

  1. 爬取1页内容
  2. 按照给定列表拼出链接爬取多页
  3. 找到‘下一页'标签进行爬取
  4. 进入链接,按照链接进行爬取

下面分别给出了示例

1.爬取1页内容

#by 寒小阳(hanxiaoyang.ml@gmail.com)

import scrapy


class JulyeduSpider(scrapy.Spider):
  name = "julyedu"
  start_urls = [
    'https://www.julyedu.com/category/index',
  ]

  def parse(self, response):
    for julyedu_class in response.xpath('//div[@class="course_info_box"]'):
      print julyedu_class.xpath('a/h4/text()').extract_first()
      print julyedu_class.xpath('a/p[@class="course-info-tip"][1]/text()').extract_first()
      print julyedu_class.xpath('a/p[@class="course-info-tip"][2]/text()').extract_first()
      print response.urljoin(julyedu_class.xpath('a/img[1]/@src').extract_first())
      print "\n"

      yield {
        'title':julyedu_class.xpath('a/h4/text()').extract_first(),
        'desc': julyedu_class.xpath('a/p[@class="course-info-tip"][1]/text()').extract_first(),
        'time': julyedu_class.xpath('a/p[@class="course-info-tip"][2]/text()').extract_first(),
        'img_url': response.urljoin(julyedu_class.xpath('a/img[1]/@src').extract_first())
      }

2.按照给定列表拼出链接爬取多页

#by 寒小阳(hanxiaoyang.ml@gmail.com)

import scrapy


class CnBlogSpider(scrapy.Spider):
  name = "cnblogs"
  allowed_domains = ["cnblogs.com"]
  start_urls = [
    'http://www.cnblogs.com/pick/#p%s' % p for p in xrange(1, 11)
    ]

  def parse(self, response):
    for article in response.xpath('//div[@class="post_item"]'):
      print article.xpath('div[@class="post_item_body"]/h3/a/text()').extract_first().strip()
      print response.urljoin(article.xpath('div[@class="post_item_body"]/h3/a/@href').extract_first()).strip()
      print article.xpath('div[@class="post_item_body"]/p/text()').extract_first().strip()
      print article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/a/text()').extract_first().strip()
      print response.urljoin(article.xpath('div[@class="post_item_body"]/div/a/@href').extract_first()).strip()
      print article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_comment"]/a/text()').extract_first().strip()
      print article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_view"]/a/text()').extract_first().strip()
      print ""

      yield {
        'title': article.xpath('div[@class="post_item_body"]/h3/a/text()').extract_first().strip(),
        'link': response.urljoin(article.xpath('div[@class="post_item_body"]/h3/a/@href').extract_first()).strip(),
        'summary': article.xpath('div[@class="post_item_body"]/p/text()').extract_first().strip(),
        'author': article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/a/text()').extract_first().strip(),
        'author_link': response.urljoin(article.xpath('div[@class="post_item_body"]/div/a/@href').extract_first()).strip(),
        'comment': article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_comment"]/a/text()').extract_first().strip(),
        'view': article.xpath('div[@class="post_item_body"]/div[@class="post_item_foot"]/span[@class="article_view"]/a/text()').extract_first().strip(),
      }

3.找到‘下一页'标签进行爬取

import scrapy
class QuotesSpider(scrapy.Spider):
  name = "quotes"
  start_urls = [
    'http://quotes.toscrape.com/tag/humor/',
  ]

  def parse(self, response):
    for quote in response.xpath('//div[@class="quote"]'):
      yield {
        'text': quote.xpath('span[@class="text"]/text()').extract_first(),
        'author': quote.xpath('span/small[@class="author"]/text()').extract_first(),
      }

    next_page = response.xpath('//li[@class="next"]/@herf').extract_first()
    if next_page is not None:
      next_page = response.urljoin(next_page)
      yield scrapy.Request(next_page, callback=self.parse)

4.进入链接,按照链接进行爬取

#by 寒小阳(hanxiaoyang.ml@gmail.com)

import scrapy


class QQNewsSpider(scrapy.Spider):
  name = 'qqnews'
  start_urls = ['http://news.qq.com/society_index.shtml']

  def parse(self, response):
    for href in response.xpath('//*[@id="news"]/div/div/div/div/em/a/@href'):
      full_url = response.urljoin(href.extract())
      yield scrapy.Request(full_url, callback=self.parse_question)

  def parse_question(self, response):
    print response.xpath('//div[@class="qq_article"]/div/h1/text()').extract_first()
    print response.xpath('//span[@class="a_time"]/text()').extract_first()
    print response.xpath('//span[@class="a_catalog"]/a/text()').extract_first()
    print "\n".join(response.xpath('//div[@id="Cnt-Main-Article-QQ"]/p[@class="text"]/text()').extract())
    print ""
    yield {
      'title': response.xpath('//div[@class="qq_article"]/div/h1/text()').extract_first(),
      'content': "\n".join(response.xpath('//div[@id="Cnt-Main-Article-QQ"]/p[@class="text"]/text()').extract()),
      'time': response.xpath('//span[@class="a_time"]/text()').extract_first(),
      'cate': response.xpath('//span[@class="a_catalog"]/a/text()').extract_first(),
    }

总结

以上就是本文关于scrapy spider的几种爬取方式实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
Python中变量交换的例子
Aug 25 Python
Python脚本实现下载合并SAE日志
Feb 10 Python
详解Django中的权限和组以及消息
Jul 23 Python
Python 正则表达式实现计算器功能
Apr 29 Python
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
Jun 23 Python
利用Opencv中Houghline方法实现直线检测
Feb 11 Python
python实现Zabbix-API监控
Sep 17 Python
python实现浪漫的烟花秀
Jan 30 Python
python程序控制NAO机器人行走
Apr 29 Python
python实现大战外星人小游戏实例代码
Dec 26 Python
python多线程semaphore实现线程数控制的示例
Aug 10 Python
Python 字典一个键对应多个值的方法
Sep 29 Python
scrapy爬虫完整实例
Jan 25 #Python
python实现画圆功能
Jan 25 #Python
Python中常用信号signal类型实例
Jan 25 #Python
简单实现python画圆功能
Jan 25 #Python
Python中sort和sorted函数代码解析
Jan 25 #Python
django在接受post请求时显示403forbidden实例解析
Jan 25 #Python
Python微信公众号开发平台
Jan 25 #Python
You might like
PHP 缓存实现代码及详细注释
2010/05/16 PHP
老生常谈php 正则中的i,m,s,x,e分别表示什么
2017/03/02 PHP
CL vs ForZe BO5 第五场 2.13
2021/03/10 DOTA
js函数般调用正则
2008/04/08 Javascript
通过javascript的匿名函数来分析几段简单有趣的代码
2010/06/29 Javascript
使用 JScript 创建 .exe 或 .dll 文件的方法
2011/07/13 Javascript
event.X和event.clientX的区别分析
2011/10/06 Javascript
javascript读写XML实现广告轮换(兼容IE、FF)
2013/08/09 Javascript
jquery css 设置table的奇偶行背景色示例
2014/06/03 Javascript
javascript实现浏览器窗口传递参数的方法
2014/09/03 Javascript
JS实现图片放大镜效果的方法
2015/02/27 Javascript
简介JavaScript中toUpperCase()方法的使用
2015/06/06 Javascript
Jquery数字上下滚动动态切换插件
2015/08/08 Javascript
JavaScript实现的浏览器下载文件的方法
2017/08/09 Javascript
JavaScript回调函数callback用法解析
2020/01/14 Javascript
Js图片点击切换轮播实现代码
2020/07/27 Javascript
Vue中用JSON实现刷新界面不影响倒计时
2020/10/26 Javascript
[01:32]DOTA2 2015国际邀请赛中国区预选赛第四日战报
2015/05/29 DOTA
python实现删除文件与目录的方法
2014/11/10 Python
python魔法方法-自定义序列详解
2016/07/21 Python
Python爬虫实例扒取2345天气预报
2018/03/04 Python
python3 实现对图片进行局部切割的方法
2018/12/05 Python
对python3 中方法各种参数和返回值详解
2018/12/15 Python
PyQt5固定窗口大小的方法
2019/06/18 Python
python调用并链接MATLAB脚本详解
2019/07/05 Python
DJango的创建和使用详解(默认数据库sqlite3)
2019/11/18 Python
Python 中判断列表是否为空的方法
2019/11/24 Python
使用Python打造一款间谍程序的流程分析
2020/02/21 Python
Python编写单元测试代码实例
2020/09/10 Python
西班牙多品牌鞋店连锁店:Krack
2018/11/30 全球购物
西班牙在线药店:DosFarma
2020/03/28 全球购物
网络编辑岗位职责范本
2014/02/10 职场文书
省级青年文明号申报材料
2014/05/23 职场文书
2014年团员学习十八大思想汇报
2014/09/13 职场文书
扶贫办主任查摆“四风”问题个人对照检查材料思想汇报
2014/10/02 职场文书
上市公司财务总监岗位职责
2015/04/03 职场文书