杂谈:

之前用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篇)的更多相关文章

  1. Python爬虫 —— 抓取美女图片

    代码如下: #coding:utf-8 # import datetime import requests import os import sys from lxml import etree im ...

  2. python 爬虫抓取心得

    quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...

  3. Python3简单爬虫抓取网页图片

    现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到 ...

  4. Python爬虫----抓取豆瓣电影Top250

    有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...

  5. Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储

    Python爬虫可以说是好玩又好用了.现想利用Python爬取网页股票数据保存到本地csv数据文件中,同时想把股票数据保存到MySQL数据库中.需求有了,剩下的就是实现了. 在开始之前,保证已经安装好 ...

  6. python爬虫-爬取百度图片

    python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...

  7. Python 爬虫: 抓取花瓣网图片

    接触Python也好长时间了,一直没什么机会使用,没有机会那就自己创造机会!呐,就先从爬虫开始吧,抓点美女图片下来. 废话不多说了,讲讲我是怎么做的. 1. 分析网站 想要下载图片,只要知道图片的地址 ...

  8. python爬虫抓取哈尔滨天气信息(静态爬虫)

    python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip i ...

  9. python+requests抓取页面图片

    前言: 学完requests库后,想到可以利用python+requests爬取页面图片,想到实战一下.依照现在所学只能爬取图片在html页面的而不能爬取由JavaScript生成的图片,所以我选取饿 ...

随机推荐

  1. Android之——流量管理程序演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...

  2. SPA路由机制详解(看不懂不要钱~~)

    前言 总所周知,随着前端应用的业务功能起来越复杂,用户对于使用体验的要求越来越高,单面(SPA)成为前端应用的主流形式.而大型单页应用最显著特点之一就是采用的前端路由跳转子页面系统,通过改变页面的UR ...

  3. 使用Nginx的proxy_cache缓存功能取代Squid

    Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...

  4. 图片压缩CompressUtil解析

    CompressUtil 流程图: CompressUtil 类 具体解释 public class CompressUtil { /** * 终于封装的压缩方法 * @param imgPath * ...

  5. eclipse--windowBuilder

    https://www.eclipse.org/windowbuilder/ https://www.eclipse.org/windowbuilder/download.php Documentat ...

  6. Nonblocking Memory Refresh&2018ISCA/Security& 非阻塞内存刷新

    Abstract 我们提议的非阻塞刷新工作是一次刷新内存块中的一部分数据,并在内存块中使用冗余数据,如RS码,在块中计算块的刷新/不可读数据以满足读取请求.作为概念的证明,我们将非阻塞刷新应用于服务器 ...

  7. 01-jsp与javabean

    <%@page import="java.util.Date"%><%@ page language="java" contentType=& ...

  8. CentOS搭建git服务器实测

    Git 可以使用四种主要的协议来传输数据:本地传输,SSH 协议,Git 协议和 HTTP 协议 1,安装: CentOS/Fedora: yum install git Ubuntu/Debian: ...

  9. BillBoardView自己定义控件广告板轮播

    BillBoardView自己定义控件广告板轮播 GitHub地址 compile 'com.march.billboardview:billboardview:2.0.6-beta4' BillBo ...

  10. ubuntu 12.10 笔记

    笔记 more ec_unitouch.log |grep Thread-4 筛选日志 打开命令行终端 ctrl + alt + t     查看版本号 : sudo lsb_release -a t ...