scrapy spider的几种爬取方式实例代码
时间:2019-04-13
本文章向大家介绍scrapy spider的几种爬取方式实例代码,主要包括scrapy spider的几种爬取方式实例代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本节课介绍了scrapy的爬虫框架,重点说了scrapy组件spider。
spider的几种爬取方式:
- 爬取1页内容
- 按照给定列表拼出链接爬取多页
- 找到‘下一页'标签进行爬取
- 进入链接,按照链接进行爬取
下面分别给出了示例
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的几种爬取方式实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
- java概念2
- iOS开发照片框架详解(二)-- PhotoKit
- wordcount.java
- phpmyadmin中导入文件时显示 No database selected
- ajax异步操作1
- php连接mysql数据库
- javascript入门操作
- php入门操作
- PHP中echo(),print(),print_r()的区别
- jquery中各个属性的应用
- jquery中通过鼠标获取页面坐标
- jquery中各个事件应用实例
- jquery中html、before、after、append、prepend应用
- jquery中hide、show、slideUp、slideDown、animate应用
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 算法系列1 初识算法 算法复杂性模型 算法复杂度的计算
- 树莓派基础实验32:DS1302实时时钟模块实验
- Failed to parse date ["1534467411000"]:Invalid time zone indicator '0'
- 快速排序 : 调优:3亿数据40秒,2亿数据30秒,1亿数据15秒
- Spring系列之新注解配置+Spring集成junit+注解注入
- Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop
- 线上工程启动日志不滚动了——通过查看堆栈信息排查的过程(ES批量插入)
- Java中使用方法的注意事项
- 微信小程序转发朋友圈详解
- Error: Protocol error, got "H" as reply type byte
- 树莓派基础实验33:TCRT5000红外循迹传感器实验
- 10W个Java对象有多大
- 一次线程池引发的线上故障分析
- dubbo 启动Failed to save registry store file报错
- 大数据的列式存储格式:Parquet