Python爬虫 —— 抓取美女图片(Scrapy篇)
杂谈:
之前用requests模块爬取了美女图片,今天用scrapy框架实现了一遍。
(图片尺度确实大了点,但老衲早已无恋红尘,权当观赏哈哈哈)
Item:
# -*- coding: utf-8 -*- # Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapy class GirlpicItem(scrapy.Item):
title = scrapy.Field()
image = scrapy.Field()
index = scrapy.Field()
Spider:
#coding:utf-8
from scrapy.spiders import Spider
from scrapy.http import Request
from scrapy.selector import Selector
from girlpic.items import GirlpicItem
import scrapy
import sys
reload(sys)
sys.setdefaultencoding('utf-8') class GirlpicSipder(Spider):
name = 'girlpic'
allowed_domains = [] # 允许的域名
start_urls = ["http://www.mzitu.com/all/"] def parse(self, response):
groups = response.xpath("//div[@class='main-content']//ul[@class='archives']//a")
count = 0
for group in groups:
count = count + 1
if count > 5:
return #此处小心,不要用os.exit(0)
groupUrl = group.xpath('@href').extract()[0]
title = group.xpath("text()").extract()[0]
request = scrapy.Request(url=groupUrl, callback=self.getGroup, meta={'title': title,'groupUrl':groupUrl}, dont_filter=True)
yield request def getGroup(self, response):
maxIndex = response.xpath("//div[@class='pagenavi']//span/text()").extract()[-2]
for index in range(1, int(maxIndex) + 1):
pageUrl = response.meta['groupUrl']+'/'+str(index)
meta = response.meta
meta['index'] = index
request = scrapy.Request(url=pageUrl, callback=self.getPage, meta=meta, dont_filter=True)
yield request def getPage(self, response):
imageurl = response.xpath("//div[@class='main-image']//img/@src").extract()[0] # 获取图片url
request = scrapy.Request(url=imageurl, callback=self.FormItem, meta=response.meta,dont_filter=True)
yield request def FormItem(self, response):
title = response.meta['title']
index = response.meta['index']
image = response.body
item = GirlpicItem(title=title,index=index,image=image)
yield item
PipeLine:
# -*- coding: utf-8 -*- # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import os
import codecs
import sys
reload(sys)
sys.setdefaultencoding('utf-8') class GirlpicPipeline(object): def __init__(self):
self.dirpath = u'D:\学习资料'
if not os.path.exists(self.dirpath):
os.makedirs(self.dirpath) def process_item(self, item, spider):
title = item['title']
index = item['index']
image = item['image']
groupdir = os.path.join(self.dirpath, title)
if not os.path.exists(groupdir):
os.makedirs(groupdir)
imagepath = os.path.join(groupdir, str(index) + u'.jpg')
file = codecs.open(imagepath, 'wb')
file.write(image)
file.close()
return item
Python爬虫 —— 抓取美女图片(Scrapy篇)的更多相关文章
- Python爬虫 —— 抓取美女图片
代码如下: #coding:utf-8 # import datetime import requests import os import sys from lxml import etree im ...
- python 爬虫抓取心得
quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...
- Python3简单爬虫抓取网页图片
现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到 ...
- Python爬虫----抓取豆瓣电影Top250
有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...
- Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储
Python爬虫可以说是好玩又好用了.现想利用Python爬取网页股票数据保存到本地csv数据文件中,同时想把股票数据保存到MySQL数据库中.需求有了,剩下的就是实现了. 在开始之前,保证已经安装好 ...
- python爬虫-爬取百度图片
python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...
- Python 爬虫: 抓取花瓣网图片
接触Python也好长时间了,一直没什么机会使用,没有机会那就自己创造机会!呐,就先从爬虫开始吧,抓点美女图片下来. 废话不多说了,讲讲我是怎么做的. 1. 分析网站 想要下载图片,只要知道图片的地址 ...
- python爬虫抓取哈尔滨天气信息(静态爬虫)
python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip i ...
- python+requests抓取页面图片
前言: 学完requests库后,想到可以利用python+requests爬取页面图片,想到实战一下.依照现在所学只能爬取图片在html页面的而不能爬取由JavaScript生成的图片,所以我选取饿 ...
随机推荐
- 压缩软件Snappy的安装
1.下载源码,通过编译源码安装 tar -zxvf /home/zfll/soft/snappy-1.1.2.tar.gz cd snappy-1.1.2 ./configure make sud ...
- EclipseADT(4.2) 安装 STS(spring )
因为ADT 版本是4.2, 网上找了一圈 from: https://spring.io/blog/2012/03/14/early-access-springsource-tool-suite-fo ...
- android(cm11)状态栏源代码分析(一)
(一):写在前面 近期因为工作须要,须要了解CM11中的有关于StatusBar相关的内容.总的来说,刚開始阅读其源代码的时候,是有点困难,只是通过构建相关代码的脑图和流程图,几天下来.我已经对其源代 ...
- JavDroider的作品展示
好久没有写博客了,很懊悔,尽管说实习和项目那边的任务有点多,可是我想每天抽出时间出来写一篇文章总结一下当天所习所得并不困难! 好了,今天以一篇个人作品介绍来又一次开启我的博客~ 实习单位的门户站点 一 ...
- hdu2955 Robberies (01背包)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=2955">http://acm.hdu.edu.cn/showproblem.php ...
- 一款很实用的Memcache监控工具
装了memcahce以后想对使用情况详细了解一下,如分配的内存够不够,都存了什么,经百度后发现这款工具灰常实用!此工具来自Memcache Pecl 中 http://pecl.php.net/pac ...
- Spring技术笔记(一)
一.控制反转(IoC)&依赖注入(DI) 1.控制反转: 所谓的控制反转就是应用本身不负责依赖对象的创建及维护, 依赖对象的创建及维护是由外部容器负责的. 这样控制权就由应用转移到了外部容器, ...
- C#命名空间大全详细教程
www.51rgb.com System 命名空间包含了定义数据类型.事件和事件处理程序等基本类: System.Data 命名空间包含了提供数据访问功能的命名空间和类: System.IO 命名空间 ...
- [Java开发之路](23)装箱与拆箱
1. 简单介绍 大家对基本数据类型都很熟悉.比如 int.float.double.boolean.char 等.基本数据类型是不具备对象的特性,比方基本类型不能调用方法.功能简单. ..,为了让基本 ...
- Java NIO之Charset类字符编码对象
介绍 java中使用Charset来表示编码对象 This class defines methods for creating decoders and encoders and for retri ...