Python爬虫入门-scrapy爬取唯一图库高清壁纸
来源:互联网 发布:ubuntu开机自启动mongo 编辑:程序博客网 时间:2024/05/29 09:57
首先,就是进入到唯一图库,点击上面的高清壁纸项目:
进入之后,下拉,发现是正常的下拉没有Ajax加载,拉到最后面点击末页,可以看出这个栏目里面总共有292页:
翻页看一下URL有什么变化,可以发现只有最后面代表这个页码的数字在发生变化:
打开F12,刷新,在原始请求代码里面有能进入到进入详情页的链接地址,可以抓取下来:
打开任意一张图片,进入到详情页码中,同样的F12,再刷新页面,里面有几个重要的信息我们要进行提取的,一个就是详情页的总页数,一个是标题,还有一个就是原图的下载地址,在原始请求的源代码中同样也能够找到相应的信息:
这里面还有分两种情况,一种就是总页数为一张的就不需要进行翻页,而对于总也是大于一张的图片就要再进行遍历,可以发现翻页时URL会发生变化: http://www.mmonly.cc/gqbz/dmbz/xxxxx_i.html
经过分析以后就可以写代码了:
mmonly.py:
import scrapyfrom scrapy.http import Requestfrom weiyiwang.items import WeiyiwangItemclass MmonlySpider(scrapy.Spider): name = 'mmonly' allowed_domains = ['mmonly.cc'] start_urls = ['http://www.mmonly.cc/gqbz/list_41_{}.html'.format(i) for i in range(1,293)] def parse(self, response): links=response.css('.item.masonry_brick.masonry-brick') for link in links: detail_url = link.css('.ABox a::attr(href)').extract_first() pages=link.css('.items_likes::text').re_first('共(.*)张') if pages==1: url=detail_url yield Request(url=url, callback=self.parse_detail) else: for i in range(1,int(pages)): url=detail_url.split('.html')[0]+'_{}.html'.format(i) yield Request(url=url,callback=self.parse_detail) def parse_detail(self,response): item=WeiyiwangItem() item['title']=response.css('.wrapper.clearfix.imgtitle h1::text').extract_first() item['img_url']=response.css('.big-pic a img::attr(src)').extract_first() yield item
最后的结果是存储至Mongodb:
pipeline.py:
import pymongoclass MongoPipeline(object): def __init__(self,mongo_uri,mongo_db): self.mongo_uri=mongo_uri self.mongo_db=mongo_db @classmethod def from_crawler(cls,crawler): return cls( mongo_uri=crawler.settings.get('MONGO_URI'), mongo_db=crawler.settings.get('MONGO_DB') ) def open_spider(self,spider): self.client=pymongo.MongoClient(self.mongo_uri) self.db=self.client[self.mongo_db] def close_spider(self,spider): self.client.close() def process_item(self, item, spider): self.db['weiyi'].insert(dict(item)) return item
阅读全文
0 0
- Python爬虫入门-scrapy爬取唯一图库高清壁纸
- Python爬虫之高清壁纸下载
- Python爬虫入门-python之爬取pexels高清图片
- python爬虫----图片爬取之高清原图
- Python Scrapy爬虫入门
- Python3 大型网络爬虫实战 003 — scrapy 大型静态图片网站爬虫项目实战 — 实战:爬取 169美女图片网 高清图片
- Scrapy爬虫入门——爬取dmoz.org数据
- Python爬虫入门-scrapy爬取拉勾网
- Python 采用Scrapy爬虫框架爬取豆瓣电影top250
- Python爬虫实战:Scrapy豆瓣电影爬取
- Python爬虫框架Scrapy:爬取校花网
- Python爬虫:用Scrapy框架爬取漫画
- python爬虫scrapy之贷联盟黑名单爬取
- Python 爬虫入门 1 了解爬虫Scrapy
- Python爬虫从入门到放弃(十九)之 Scrapy爬取所有知乎用户信息(下)
- python爬虫入门之爬取大学排名
- python3 [爬虫入门实战]爬虫之scrapy爬取中国医学人才网
- python3 [爬虫入门实战]爬虫之scrapy爬取中华人民共和国民政部
- hdu2433 BFS最短路
- codeforces 585F. Digits of Number Pi
- [日推荐]『装逼助手』一秒变土豪
- nutz从eclipse转idea的问题总结
- 表变量与临时表对比(2)
- Python爬虫入门-scrapy爬取唯一图库高清壁纸
- 管理和维护表
- JAVA常见问题解决办法汇总
- eval()和json.parse()的区别
- 【特斯拉组件】iOS高性能PageController
- 用struts标签完成一个注册页面
- 学习总结6
- python 操作Excel openpyxl的使用
- in运算符-----js