系列文章列表:

scrapy爬虫学习系列一:scrapy爬虫环境的准备:       http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_007_scrapy01.html

scrapy爬虫学习系列二:scrapy简单爬虫样例学习:  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_007_scrapy02.html

scrapy爬虫学习系列三:scrapy部署到scrapyhub上:   http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_004_scrapyhub.html

scrapy爬虫学习系列四:portia的学习入门:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_010_scrapy04.html

scrapy爬虫学习系列五:图片的抓取和下载:                 http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_011_scrapy05.html

scrapy爬虫学习系列六:官方文档的学习:                     https://github.com/zhaojiedi1992/My_Study_Scrapy

注意: 我自己新建的一个QQ群(新建的),欢迎大家加入一起学习一起进步 ,群号646187336

这篇文章主要对一个车标网(http://car.bitauto.com/qichepinpai)的图片进行抓取,并按照图片的alt属性值去设置输出图片命名。

本文的最终源码下载地址(github):https://github.com/zhaojiedi1992/caricon

1.创建工程和爬虫

C:\Users\Administrator>e:

E:\>cd scrapytest

E:\scrapytest>scrapy startproject caricon
New Scrapy project 'caricon', using template directory 'C:\\Program Files\\Anaconda3\\lib\\site-packages\\scrapy\\templa
tes\\project', created in:
E:\scrapytest\caricon You can start your first spider with:
cd caricon
scrapy genspider example example.com E:\scrapytest>cd caricon E:\scrapytest\caricon>scrapy genspider car car.bitauto.com/qichepinpai
Created spider 'car' using template 'basic' in module:
caricon.spiders.car

4.修改item

添加字段,修改后为如下内容:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html import scrapy class CariconItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
image_urls = scrapy.Field()
images = scrapy.Field()
alt = scrapy.Field()
  • image_urls : 作为项目的图片网址(需要我们指定url)。
  • images :下载的影像信息(这个字段不是我们填充的)。

注意: 上面的alt字段是我自己加的,image_urls ,images这2个字段是请求图片的默认字段,必须要有的,建议使用默认字段。你要是喜欢折腾可以参考这个网址:https://docs.scrapy.org/en/latest/topics/media-pipeline.html#usage-example

3.修改爬虫

这里我们先使用火狐浏览器的Firefinder插件找找我们需要提取的图片,图片如下:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html class CariconPipeline(object):
def process_item(self, item, spider):
return item
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.http import Request
from scrapy.exceptions import DropItem
import os class MyImagesPipeline(ImagesPipeline):
def file_path(self, request, response=None, info=None):
#url_file_name= request.url.split('/')[-1]
#image_guid = hashlib.sha1(to_bytes(url)).hexdigest()
alt_name=request.meta["alt"]
return 'full/%s%s' % (alt_name, os.path.splitext(request.url)[-1]) def get_media_requests(self, item, info):
yield Request(item["image_urls"][0], meta={'alt':item["alt"]})

代码简介:通常我们使用官方的那个imagepipeline导出的文件是SHA1 hash 你的url作为文件名,很难区别啊,这里使用到了request方法的meta参数,把我们的图片的alt属性传递过去,这样我们返回文件名的时候就可以使用这个alt的名字来区别了。(但是如果alt重复又替换了原来的图片的)

注意,firefinder这个插件依赖与firebug的,你可以在你的浏览器找类似firefinder的工具。

6.修改setttings.py文件

修改下面片段为如下内容:

ITEM_PIPELINES = {
'caricon.pipelines.MyImagesPipeline': 300,
}
IMAGES_STORE = r'e:\test\pic\'

当然我们这里可以使用官方的imagepipeline(scrapy.pipelines.images.ImagesPipeline)

6.运行爬虫

E:\scrapytest\caricon>scrapy crawl car

7.查看结果

scrapy爬虫学习系列五:图片的抓取和下载的更多相关文章

  1. scrapy爬虫学习系列四:portia的学习入门

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  2. scrapy爬虫学习系列二:scrapy简单爬虫样例学习

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  3. scrapy爬虫学习系列一:scrapy爬虫环境的准备

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  4. scrapy爬虫学习系列三:scrapy部署到scrapyhub上

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  5. scrapy爬虫学习系列七:scrapy常见问题解决方案

    1 常见错误 1.1 错误: ImportError: No module named win32api 官方参考:https://doc.scrapy.org/en/latest/faq.html# ...

  6. Scrapy爬虫框架教程(四)-- 抓取AJAX异步加载网页

    欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章 sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction ...

  7. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  8. [转]《Python爬虫学习系列教程》

    <Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...

  9. python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容

    python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI,采用Python语言编写 ...

随机推荐

  1. That girl

    音标 词汇 Purple Glasses Black Hat Brown Bag Clothes Blue Jacket Pink Handbag Sock White Skirt Shoe 1, s ...

  2. 1、初识Java

    1.计算机发展过程 2.Java简史 lSUN公司是一家什么样的公司? 美国SUN(Stanford University Network)公司 在中国大陆的正式中文名为“太阳计算机系统(中国)有限公 ...

  3. sql server创建登录出发器后导致登录失败--解决方案

    1.选择sql server配置管理器---sql server服务--右键属性--启动参数--添加-f.-m两个参数并重启sql server服务 2.重新启动sql server以windos身份 ...

  4. cookie跟session自我介绍

    Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...

  5. FCC(ES6写法) Exact Change

    设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. cid  ...

  6. svn错误:更新源码出现校验和不匹配问题

    svn 的文本校验和不匹配: 最近在更新自动化源代码的时候出现了一个错误:svn: Checksum mismatch while updating.... 查了下google,原来是在更新源码出现校 ...

  7. [Swift]LeetCode726. 原子的数量 | Number of Atoms

    Given a chemical formula (given as a string), return the count of each atom. An atomic element alway ...

  8. [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  9. 浅谈React

    浅谈react react是什么?其官网给出了明确定义:A JavaScript library for building user interfaces,一个用于构建用户界面的JavaScript库 ...

  10. 『高次同余方程 Baby Step Giant Step算法』

    高次同余方程 一般来说,高次同余方程分\(a^x \equiv b(mod\ p)\)和\(x^a \equiv b(mod\ p)\)两种,其中后者的难度较大,本片博客仅将介绍第一类方程的解决方法. ...