[Python_scrapy图片爬取下载]
welcome to myblog
爬取某个车站的图片
item.py 中
1、申明item 的fields
class PhotoItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
image_urls = scrapy.Field()
images = scrapy.Field()
pass
spider 的image.py
导入头文件
from Photo.items import PhotoItem from scrapy import Spider from scrapy import Selector from scrapy.http import Request
爬取代码
注:
只需要爬取图片对应的url
翻页爬取时加上爬取范围url
class imageSpider(Spider):
name = 'car'
allowed_domains = ['car.autohome.com.cn']
start_urls = [
"https://car.autohome.com.cn/jingxuan/list-0-p1.html",
]
def parse(self, response):
item = PhotoItem()
sel = Selector(response)
item['image_urls'] = sel.xpath('//ul[@class="content"]/li/a/img/@src').extract()
print item['image_urls'], '..image_urls..'
yield item
# 翻页
new_urls = response.xpath('//div[@class="pageindex"]/a[9]/@href').extract_first()
new_url = "https://car.autohome.com.cn" + new_urls
print new_url, '..new_url...'
if new_url:
yield Request(new_url, callback=self.parse)
settings.py 中
大专栏 [Python_scrapy图片爬取下载]Configure item pipelines
ITEM_PIPELINES = {
'Photo.pipelines.jandanPipeline': 200,
# 'Photo.pipelines.PhotoPipeline': 300,
}
存储下载图片所在位置
IMAGES_STORE = '/Users/sansi/Desktop/Scrapy/Photo/Image'
DOWNLOAD_DELAY = 0.25
缩略图大小
IMAGES_THUMBS = {
'small': (50, 50),
'big': (200, 200),
}
图片的失效期限
IMAGES_EXPIRES = 90
pipelines.py 中
导入头文件
import os
import urllib
import scrapy
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline
from Photo import settings
编写爬取下载
class PhotoPipeline(object):
def process_item(self, item, spider):
return item
重写ImagesPipeline,对各个url返回Request
class jandanPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['image_urls']:
yield scrapy.Request(image_url)
当一个项目所有的请求完成时调用
def item_completed(self, results, item, info):
image_paths = [x['path'] for ok, x in results if ok]
if not image_paths:
raise DropItem("Item contains no images")
item['images'] = image_paths
return item
[Python_scrapy图片爬取下载]的更多相关文章
- Python爬虫入门教程 26-100 知乎文章图片爬取器之二
1. 知乎文章图片爬取器之二博客背景 昨天写了知乎文章图片爬取器的一部分代码,针对知乎问题的答案json进行了数据抓取,博客中出现了部分写死的内容,今天把那部分信息调整完毕,并且将图片下载完善到代码中 ...
- 4k图片爬取+中文乱码
4k图片爬取+中文乱码 此案例有三种乱码解决方法,推荐第一种 4k图片爬取其实和普通图片爬取的过程是没有本质区别的 import requests import os from lxml import ...
- 爬虫07 /scrapy图片爬取、中间件、selenium在scrapy中的应用、CrawlSpider、分布式、增量式
爬虫07 /scrapy图片爬取.中间件.selenium在scrapy中的应用.CrawlSpider.分布式.增量式 目录 爬虫07 /scrapy图片爬取.中间件.selenium在scrapy ...
- scrapy之360图片爬取
#今日目标 **scrapy之360图片爬取** 今天要爬取的是360美女图片,首先分析页面得知网页是动态加载,故需要先找到网页链接规律, 然后调用ImagesPipeline类实现图片爬取 *代码实 ...
- Python爬虫入门教程 25-100 知乎文章图片爬取器之一
1. 知乎文章图片写在前面 今天开始尝试爬取一下知乎,看一下这个网站都有什么好玩的内容可以爬取到,可能断断续续会写几篇文章,今天首先爬取最简单的,单一文章的所有回答,爬取这个没有什么难度. 找到我们要 ...
- Python爬虫入门教程 8-100 蜂鸟网图片爬取之三
蜂鸟网图片--啰嗦两句 前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用aiohttp 希望你喜欢 爬取页面https://tu.fengniao.com/15/ 本篇教程还 ...
- Python爬虫入门教程 7-100 蜂鸟网图片爬取之二
蜂鸟网图片--简介 今天玩点新鲜的,使用一个新库 aiohttp ,利用它提高咱爬虫的爬取速度. 安装模块常规套路 pip install aiohttp 运行之后等待,安装完毕,想要深造,那么官方文 ...
- Python爬虫入门教程 6-100 蜂鸟网图片爬取之一
1. 蜂鸟网图片--简介 国庆假日结束了,新的工作又开始了,今天我们继续爬取一个网站,这个网站为 http://image.fengniao.com/ ,蜂鸟一个摄影大牛聚集的地方,本教程请用来学习, ...
- Python爬虫入门教程 5-100 27270图片爬取
27270图片----获取待爬取页面 今天继续爬取一个网站,http://www.27270.com/ent/meinvtupian/ 这个网站具备反爬,so我们下载的代码有些地方处理的也不是很到位, ...
随机推荐
- Linux shell脚本 基础
一.shell中三个引号的用法 1.单引号:所见即所得 例如:var=123 var2='${var}123' echo var2 var2结果为${var}123 2.双引号:输出引号中的内容,若存 ...
- Java使用Thrift,Thrift结构体定义
1.Thrift定义文件,Thrift常见的数据类型 1.基本类型(括号内为对应的Java类型): bool(boolean): 布尔类型(TRUE or FALSE) byte(byte): 8位带 ...
- Debian8.8下的VIM的配置文件
传动们:http://blog.csdn.net/gatieme/article/details/43883261?spm=5176.100239.blogcont47532.3.yXiEuB 感觉挺 ...
- HEX 文件格式
例 FDFFF885C3 :每行开头 第一个字节:表示本行的数据长度, 第二个,第三个字节表示本行数据的起始地址. 第四字节表示数据类型,数据类型有:0x00.0x01.0x02.0x03.0x04. ...
- linear correlation coefficient|Correlation and Causation|lurking variables
4.4 Linear Correlation 若由SxxSyySxy定义则为: 所以为了计算方便: 所以,可以明白的是,Sxx和Sx是不一样的! 所以,t r is independent of th ...
- cas sso单点登录 登录过程和登出过程原理说明
CAS大体原理我就不说了,网上一大把,不过具体交互流程没说清楚,所以有这篇文章,如果有错误,请多多指教 登录过程 用户第一次访问一个CAS 服务的客户web 应用时(访问URL :http://192 ...
- 01-电子商城项目介绍及ssm框架搭建
1.B2C电商项目功能及架构 1.1功能列表 1.2系统架构(soa架构) 2.后台管理系统工程搭建及测试 ypMall,ypMall-manager-web ypMall为父项目,管理子项目的jar ...
- python之接口开发
一.接口开发的思路 1.启动一个服务: 2.接受客户端传过来的数据: 3.登录,注册,支付等功能 4.操作数据库,拿到数据: 5.返回数据: import flask server=flask.Fla ...
- VisualStudioAddin2016Setup.rar
本工具是用于Visual Studio 2010 /2012 的外接程序. 功能不太多,常用代码,引用管理等. 动态图: 下载地址: VisualStudioAddin2016Setup.rar
- php7 安装mongodb扩展
下载 mongodb-1.6.0.tgz wget https://pecl.php.net/get/mongodb-1.6.0.tgz 版本太低的话有些语法不一样,起码1.5以上吧 进入 mo ...