杂谈:

之前用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. mysql数据库查看各实例磁盘占用情况

    1.总体查看: use information_schema; select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' ...

  2. 2016.10.19 intelliJ的基本操作

    参考大部分来自:IntelliJ IDEA 13试用手记(附详细截图) 用eclipse实在用的有点心累了.所以准备转战intelliJ.   一.下载安装 官网地址:http://www.jetbr ...

  3. android listView 滑动载入数据 该数据是服务端获取的

    package com.sunway.works.applycash; import java.util.ArrayList; import java.util.Calendar; import ja ...

  4. SSO单点登录系列1:cas客户端源码分析cas-client-java-2.1.1.jar

    落雨 cas 单点登录 希望能给以后来研究cas的兄弟留下一点思路,也算是研究了两天的成果,外国人的代码写的很晦涩,翻译下来也没有时间继续跟进,所以有错误的还请大家跟帖和我讨论,qq 39426378 ...

  5. odoo图片显示

        如果在odoo客户端展示图片, 可以用 url( data:image/png;base64, 图片base64编码过的内容) 展示, 例如     url(data:image/png;ba ...

  6. 数据存储之Archiver、Unarchiver、偏好设置

    数组的归档 对象的归档 NSData多个对象的归档 NSArray多个对象的归档 偏好设置的存储 1.NSString.NSDictionary.NSArray.NSData.NSNumber等类型的 ...

  7. Deploying Docker images via SSH

    Original URL:https://advancedweb.hu/2015/04/14/deploying-docker-images-via-ssh/ Background When we b ...

  8. aar格式

    aar包是Android Library Project的二进制公布包. 文件的扩展名是aar,并且maven包类型也应该是aar. 只是这文件本身就是一个简单的zip文件.里面有例如以下的内容: / ...

  9. LeetCode 之 Valid Palindrome(字符串)

    [问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...

  10. java 中的final

    在编程语言中都有某种方式,告知编译器一块数据是恒定不变的.有两个需求 1. 一个永不改变的编译器常量 2. 一个在运行时被初始化的值,而这个值不会被改变 在Java中,使用final修饰变量实现这两个 ...