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中使用itertools模块中的组合函数的教程
Apr 13 Python
浅析Python编写函数装饰器
Mar 18 Python
Python编程实现粒子群算法(PSO)详解
Nov 13 Python
Python迭代器定义与简单用法分析
Apr 30 Python
python pycurl验证basic和digest认证的方法
May 02 Python
通过Pandas读取大文件的实例
Jun 07 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
Jul 18 Python
numpy中的meshgrid函数的使用
Jul 31 Python
Python 将 QQ 好友头像生成祝福语的实现代码
May 03 Python
记一次Django响应超慢的解决过程
Sep 17 Python
python matlab库简单用法讲解
Dec 31 Python
学会Python数据可视化必须尝试这7个库
Jun 16 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删除目录及目录下所有文件的方法详解
2013/06/06 PHP
非常全面的php日期时间运算汇总
2015/11/04 PHP
php简单获取复选框值的方法
2016/05/11 PHP
thinkphp在低版本Nginx 下支持PATHINFO的方法分享
2016/05/27 PHP
JQuery UI的拖拽功能实现方法小结
2012/03/14 Javascript
JavaScript 判断浏览器是否支持SVG的代码
2013/03/21 Javascript
jQuery中获取Radio元素值的方法
2013/07/02 Javascript
jQuery中clearQueue()方法用法实例
2014/12/29 Javascript
随机生成10个不重复的0-100的数字(实例讲解)
2017/08/16 Javascript
webpack 插件html-webpack-plugin的具体使用
2018/04/09 Javascript
vuex如何重置所有state(可定制)
2019/01/17 Javascript
详解Vue 匿名、具名和作用域插槽的使用方法
2019/04/22 Javascript
Vue.js组件props数据验证实现详解
2019/10/19 Javascript
详解Webpack抽离第三方类库以及common解决方案
2020/03/30 Javascript
[02:28]DOTA2英雄基础教程 狼人
2013/12/23 DOTA
[00:12]DAC2018 no[o]ne亮相SOLO赛 他是否如他的id一样无人可挡?
2018/04/06 DOTA
用Python计算三角函数之atan()方法的使用
2015/05/15 Python
Centos7 Python3下安装scrapy的详细步骤
2018/03/15 Python
带你认识Django
2019/01/15 Python
Python 的AES加密与解密实现
2019/07/09 Python
Numpy之将矩阵拉成向量的实例
2019/11/30 Python
python 实现Flask中返回图片流给前端展示
2020/01/09 Python
python语言是免费还是收费的?
2020/06/15 Python
python类共享变量操作
2020/09/03 Python
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
2020/11/25 Python
John Hardy官方网站:手工设计首饰的奢侈品牌
2017/07/05 全球购物
意大利奢华内衣制造商:Cosabella
2017/08/29 全球购物
简历自我评价模版
2014/01/31 职场文书
岗位竞聘书范文
2014/03/31 职场文书
广播体操比赛口号
2014/06/10 职场文书
市政工程技术专业自荐书
2014/07/06 职场文书
新颖的化妆品活动方案
2014/08/21 职场文书
标准大学生职业生涯规划书写作指南
2014/09/18 职场文书
退学证明范本3篇
2014/10/29 职场文书
Python各协议下socket黏包问题原理
2022/04/12 Python
apache虚拟主机配置的三种方式(小结)
2022/07/23 Servers