[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我们下载的代码有些地方处理的也不是很到位, ...
随机推荐
- TPO6-1 Powering the Industrial Revolution
By 1800 more than a thousand steam engines were in use in the British Isles, and Britain retained a ...
- play framework在eclipse中自动的预编译生成precompiled文件
一.修改 eclipe 中的启动文件属性eclipse/*.launch 中的最后一条:加入参数 -Dprecompile=yes 将会在启动项目时,进行项目的预编译 (将在项目中生成 precom ...
- 4418开发板Android源码整体&单独编译
内核缺省文件配置之后,Android 目录,运行一键编译脚本“build_android.sh”,如下图所示.注意:本篇文章基于iTOP-4418开发板,编译 Android 必须保证给 Ubuntu ...
- iOS商品详情、ffmpeg播放器、指示器集锦、自定义圆弧菜单、实用工具等源码
iOS精选源码 电商商品详情 Swift.两种方式实现tableViewCell拖拽功能 ffmpeg+openGL播放器 微信聊天表情雨.表情下落.表情动画 iOS指示器集锦 弹窗增加 picker ...
- jmeter测试get post 笔记
0 环境 系统环境:win7 1 操作 1 post 新建线程组 2 get 和post新建类似 http请求 只是新建一个参数化我测试的2个url http://127.0.0.1:8080/cry ...
- Linux_新建用户
目录 1.新增用户 2.增加密码 新增用户:cn 进入root 输入新建命令 cn就是我们的新的用户名,也可以换成其他的 sudo useradd cn 接下来发现没有反应,是正常的,如图 查看是否新 ...
- Python-SSH批量登陆并执行命令
Python-SSH批量登陆并执行命令 #!/usr/bin/env python #-*- coding:utf-8 -*- import paramiko from time import cti ...
- python练习题——猜数字游戏
增加了按照对半找数的方法来计算最短几次就可以猜到随机数,决定到游戏结束共猜数的次数: from random import * import numpy as np from numpy import ...
- resume|issue|transmit|sake|obliged|beyond her wildest dreams|echo|transmission|immense|consistent |convey to| boasted|satisfaction|rub|enrol|demonize
If an activity resumes, or if you resume it, it startsagain after a pause. (中断后)继续,重新开始 Normal servi ...
- Java IO: System.in, System.out, System.err
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) System.in, System.out, System.err这3个流同样是常见的数据来 ...