黑松山资源网 Design By www.paidiu.com
本节课介绍了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的几种爬取方式实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月08日
2024年10月08日
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍
- 魔兽世界无畏远征军声望军需官在哪 wlk无畏远征军声望军需官坐标位置
- 逼迫中国选手弃权事件持续发酵 玩家抵制万代模型
- B社老员工在《星空》开发期间离职 他终于说出原因