系列文章列表:

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. iptv

    # -*- coding: utf-8 -*- import datetime, time, json, re, os #from pwd import getpwnam #quality str_q ...

  2. hy这个破项目

    最近部署hy记事 这段时间摊上了个挺恶心的项目,叫什么hy鞋同平台..前后左右整的人挺难受的.学到的东西特别少,而且比较浪费时间.不过,还是总结一下吧,好歹花了这么久的时间了 Doc管理xi tong ...

  3. diy 滚动条 样式 ---- 核心代码

    参考自 : https://blog.csdn.net/qq_38881495/article/details/83689721 .chapter_data position relative wid ...

  4. CVE-2018-20129:DedeCMS V5.7 SP2前台文件上传漏洞

    一.漏洞摘要 漏洞名称: DedeCMS V5.7 SP2前台文件上传漏洞上报日期: 2018-12-11漏洞发现者: 陈灿华产品首页: http://www.dedecms.com/软件链接: ht ...

  5. Oracle数据库在plsql中文乱码,显示问号????

    1.错误显示结果: 2.错误的原因: SqlPlus乱码与操作系统用户中的NLS_LANG这个环境变量有关系,如果这个与数据库字符集不一致的话就会产生乱码 3.解决方法1:(配置环境变量) 1.sel ...

  6. RaspberryPi上建立wordpress

    准备工作: 1.RaspberryPi 3代 B型 2.可用内存卡 3.读卡器 4.DiskGenius 5.Win32 Disk Imager 6.可用局域网 7.Xshell 和 Xftp 8.官 ...

  7. linux 重新生成网卡配置文件

    nmcli connection add con-name home type ethernet ifname eth1 autoconnect yes ip4 10.1.252.60/24 gw4 ...

  8. Echarts自定义tootips

    由于业务需求,现在要自定义tootips; 设计稿如下: 代码如下: app.title = '坐标轴刻度与标签对齐'; var str1 = "top:-20px;border:0px s ...

  9. [Swift]LeetCode68. 文本左右对齐 | Text Justification

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  10. [Swift]LeetCode688. “马”在棋盘上的概率 | Knight Probability in Chessboard

    On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K ...