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生成的图片,所以我选取饿 ...
随机推荐
- iOS开发--从TQRichTextViewDemo中学会分析project
下载地址: http://code4app.com/ios/TQRichTextView/5244fe9c6803fa0862000000 1.首先找到AppDelegate类.不管一个project ...
- 连接Zookeeper操作
public class ZKConnector implements Watcher{ private static final Logger logger =LoggerFactory.getLo ...
- 转:代码管理技巧——两步创建本地SVN服务器图文教程
from: http://www.cnblogs.com/tianhonghui/archive/2012/07/22/2603454.html 当我们进行开发的时候,不论是独立开发还是处在团队中 ...
- VC++动态链接库(DLL)编程深入浅出(二)
好,让我们正式进入动态链接库的世界,先来看看最一般的DLL,即非MFC DLL 上节给大家介绍了静态链接库与库的调试与查看,本节主要介绍非MFC DLL. 4.非MFC DLL 4.1一个简单的DLL ...
- Web自动化测试框架改进
Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 一.适用范围:传统Web功能自动化测试.H5功能自动化测试 ...
- Webpack DLL
参考自:https://www.jianshu.com/p/a5b3c2284bb6 在用 Webpack 打包的时候,对于一些不经常更新的第三方库,比如 react,lodash,我们希望能和自己的 ...
- Java函数的基本知识
http://blog.csdn.net/cxwen78/article/details/7322891主要从Java函数的定义,函数的特点,函数的应用,函数的重载四个方面来讲解Java函数. 一.函 ...
- java中的值传递和引用传递区别
值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...
- Allegro Desgin Compare的用法与网表比较
转:Allegro Desgin Compare的用法与网表比较 Allegro中自带有Design Compare工具,利用它可以比较明了的看到线路的差异.当然也可以通过SKILL进行比较,不过我们 ...
- AMD和Intel的CPU对比
http://www.lotpc.com/yjzs/5825.html 推荐文章:小白看AMD与intel的cpu架构,AMD慢的原因 CPU核心的发展方向是更低的电压.更低的功耗.更先进的制造工艺. ...