python网络爬虫之使用scrapy爬取图片
在前面的章节中都介绍了scrapy如何爬取网页数据,今天介绍下如何爬取图片。
下载图片需要用到ImagesPipeline这个类,首先介绍下工作流程:
1 首先需要在一个爬虫中,获取到图片的url并存储起来。也是就是我们项目中test_spider.py中testSpider类的功能
2 项目从爬虫返回,进入到项目通道也就是pipelines中
3 在通道中,在第一步中获取到的图片url将被scrapy的调度器和下载器安排下载。
4 下载完成后,将返回一组列表,包括下载路径,源抓取地址和图片的校验码
大致的过程就以上4步,那么我们来看下代码如何具体实现
1 首先在settings.py中设置下载通道,下载路径以下载参数
ITEM_PIPELINES = {
# 'test1.pipelines.Test1Pipeline': 300,
'scrapy.pipelines.images.ImagesPipeline':1,
}
IMAGES_STORE ='E:\\scrapy_project\\test1\\image'
IMAGES_EXPIRES = 90
IMAGES_MIN_HEIGHT = 100
IMAGES_MIN_WIDTH = 100
其中IMAGES_STORE是设置的是图片保存的路径。IMAGES_EXPIRES是设置的项目保存的最长时间。IMAGES_MIN_HEIGHT和IMAGES_MIN_WIDTH是设置的图片尺寸大小
2 设置完成后,我们就开始写爬虫程序,也就是第一步获取到图片的URL。我们以http://699pic.com/people.html网站图片为例。中文名称为摄图网。里面有各种摄影图片。我们首先来看下网页结构。图片的地址都保存在
<div class=“swipeboxex”><div class=”list”><a><image>中的属性data-original
首先在item.py中定义如下几个结构体
class Test1Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
image_urls=Field()
images=Field()
image_path=Field()
根据这个网页结构,在test_spider.py文件中的代码如下。在items中保存了
class testSpider(Spider):
name="test1"
allowd_domains=['699pic.com']
start_urls=["http://699pic.com/people.html"]
print start_urls
def parse(self,response):
items=Test1Item()
items['image_urls']=response.xpath('//div[@class="swipeboxEx"]/div[@class="list"]/a/img/@data-original').extract()
return items
3 在第二步中获取到了图片url后,下面就要进入pipeline管道。进入pipeline.py。首先引入ImagesPipeline
from scrapy.pipelines.images import ImagesPipeline
然后只需要将Test1Pipeline继承自ImagesPipeline就可以了。里面可以不用写任意代码
class Test1Pipeline(ImagesPipeline):
pass
ImagesPipeline中主要介绍2个函数。get_media_requests和item_completed.我们来看下代码的实现:
def get_media_requests(self, item, info):
return [Request(x) for x in item.get(self.images_urls_field, [])]
从代码中可以看到get_meida)_requests是从管道中取出图片的url并调用request函数去获取这个url
Item_completed函数
def item_completed(self, results, item, info):
if isinstance(item, dict) or self.images_result_field in item.fields:
item[self.images_result_field] = [x for ok, x in results if ok]
return item
当下载完了图片后,将图片的路径以及网址,校验码保存在item中
下面运行代码,这里贴出log中的运行日志:
2017-06-09 22:38:17 [scrapy] INFO: Scrapy 1.1.0 started (bot: test1)
2017-06-09 22:38:17 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'test1.spiders', 'IMAGES_MIN_HEIGHT': 100, 'SPIDER_MODULES': ['test1.spiders'], 'BOT_NAME': 'test1', 'USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36', 'LOG_FILE': 'log', 'IMAGES_MIN_WIDTH': 100}
2017-06-09 22:38:18 [scrapy] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.corestats.CoreStats']
2017-06-09 22:38:18 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-06-09 22:38:18 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-06-09 22:38:19 [scrapy] INFO: Enabled item pipelines:
['scrapy.pipelines.images.ImagesPipeline']
2017-06-09 22:38:19 [scrapy] INFO: Spider opened
2017-06-09 22:38:19 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-06-09 22:38:19 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-06-09 22:38:19 [scrapy] DEBUG: Crawled (200) <GET http://699pic.com/people.html> (referer: None)
2017-06-09 22:38:19 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50004/2199.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:19 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50004/2199.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00015/5234.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00015/5234.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50002/2276.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50002/2276.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00029/2430.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00029/2430.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00002/3435.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00002/3435.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00045/2871.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00045/2871.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00037/9614.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00037/9614.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00038/9869.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00038/9869.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00021/8332.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00021/8332.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50001/6744.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50001/6744.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00031/3314.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00031/3314.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50006/3243.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50006/3243.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50000/4373.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50000/4373.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00013/4480.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00013/4480.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00043/3285.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00043/3285.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50001/1769.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50001/1769.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00002/9278.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00002/9278.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00004/4944.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00004/4944.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00014/2406.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00014/2406.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00002/3437.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00002/3437.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00022/2328.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00022/2328.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00019/6796.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00019/6796.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00007/4890.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00007/4890.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/00017/0701.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/00017/0701.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Crawled (200) <GET http://img95.699pic.com/photo/50016/6025.jpg_wh300.jpg> (referer: None)
2017-06-09 22:38:20 [scrapy] DEBUG: File (downloaded): Downloaded file from <GET http://img95.699pic.com/photo/50016/6025.jpg_wh300.jpg> referred in <None>
2017-06-09 22:38:20 [scrapy] DEBUG: Scraped from <200 http://699pic.com/people.html>
{'image_urls': [u'http://img95.699pic.com/photo/50004/2199.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00002/3435.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00038/9869.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00029/2430.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00037/9614.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/50002/2276.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00045/2871.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00015/5234.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00021/8332.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00043/3285.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/50001/6744.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/50001/1769.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00031/3314.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/50006/3243.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/50000/4373.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00013/4480.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00002/9278.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00017/0701.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00022/2328.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00019/6796.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00004/4944.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/50016/6025.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00002/3437.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00014/2406.jpg_wh300.jpg',
u'http://img95.699pic.com/photo/00007/4890.jpg_wh300.jpg'],
'images': [{'checksum': '09d39902660ad2e047d721f53e7a2019',
'path': 'full/eb33d8812b7a06fbef2f57b87acca8cbd3a1d82b.jpg',
'url': 'http://img95.699pic.com/photo/50004/2199.jpg_wh300.jpg'},
{'checksum': 'dc87cfe3f9a3ee2728af253ebb686d2d',
'path': 'full/63334914ac8fc79f8a37a5f3bd7c06abeffac2a8.jpg',
'url': 'http://img95.699pic.com/photo/00002/3435.jpg_wh300.jpg'},
{'checksum': 'b19e55369fa0a5061f48fe997b0085e5',
'path': 'full/4f06b529a4a5fd752339fc120a1bcf89a7125da0.jpg',
'url': 'http://img95.699pic.com/photo/00038/9869.jpg_wh300.jpg'},
{'checksum': '786e0cfacf113d00302794c5fda7e93f',
'path': 'full/9540903ee92ec44d59916b4a5ebcbcec32e50a67.jpg',
'url': 'http://img95.699pic.com/photo/00029/2430.jpg_wh300.jpg'},
{'checksum': 'c4266a539b046c1b8f66609b3fef36e2',
'path': 'full/ea9bda3236f78ac319b02add631d7a713535c8d0.jpg',
'url': 'http://img95.699pic.com/photo/00037/9614.jpg_wh300.jpg'},
{'checksum': '0b4d75bb3289a2bda05dac84bfee3591',
'path': 'full/1831779855e3767e547653a823a4c986869bb6df.jpg',
'url': 'http://img95.699pic.com/photo/50002/2276.jpg_wh300.jpg'},
{'checksum': '0c7b9e849acf00646ef06ae4ade0a024',
'path': 'full/cac4915246f820035198c014a770c80cb078300b.jpg',
'url': 'http://img95.699pic.com/photo/00045/2871.jpg_wh300.jpg'},
{'checksum': '4482da90e8b468e947cd2872661c6cac',
'path': 'full/118dc882386112ab593e86743222200bedb8752a.jpg',
'url': 'http://img95.699pic.com/photo/00015/5234.jpg_wh300.jpg'},
{'checksum': '12b8d957b3a1fd7a29baf41c06b17105',
'path': 'full/ac2e45ed2716678a5045866a65d86e304e97e8ad.jpg',
'url': 'http://img95.699pic.com/photo/00021/8332.jpg_wh300.jpg'},
{'checksum': '44275a83945dfe4953ecc82c7105b869',
'path': 'full/b0ec1a2775ec55931e133c46e9f1d8680bea39e6.jpg',
'url': 'http://img95.699pic.com/photo/00043/3285.jpg_wh300.jpg'},
{'checksum': 'e960e51ebbc4ac7bd12c974ca8a33759',
'path': 'full/5f2a293333ea3f1fd3c63b7d56a9f82e1d9ff4d8.jpg',
'url': 'http://img95.699pic.com/photo/50001/6744.jpg_wh300.jpg'},
{'checksum': '08acf086571823fa739ba1b0aa5c99f3',
'path': 'full/70b24f8e7e1c4b4d7e3fe7cd6056b1ac4904f92e.jpg',
'url': 'http://img95.699pic.com/photo/50001/1769.jpg_wh300.jpg'},
{'checksum': '2599ccd44c640948e5331688420ec8af',
'path': 'full/a20b9c4eaf12a56e8b5506cc2a27d28f3e436595.jpg',
'url': 'http://img95.699pic.com/photo/00031/3314.jpg_wh300.jpg'},
{'checksum': '39bcb67a642f1cc9776be934df292f59',
'path': 'full/8b3d1eee34fb752c5b293252b10f8d9793f05240.jpg',
'url': 'http://img95.699pic.com/photo/50006/3243.jpg_wh300.jpg'},
{'checksum': 'd2e554d618de6d53ffd76812bf135edf',
'path': 'full/907c628df31bf6d6f2077b5f7fd37f02ea570634.jpg',
'url': 'http://img95.699pic.com/photo/50000/4373.jpg_wh300.jpg'},
{'checksum': '6fc5c1783080cee030858b9abb5ff6a5',
'path': 'full/f42a1cca0f7ec657aa66eca9a751a0c4d8defbb1.jpg',
'url': 'http://img95.699pic.com/photo/00013/4480.jpg_wh300.jpg'},
{'checksum': '906d1b79cec6ac8a0435b2c5c9517b4a',
'path': 'full/35853ef411058171381dc65e7e2c824a86caecbe.jpg',
'url': 'http://img95.699pic.com/photo/00002/9278.jpg_wh300.jpg'},
{'checksum': '3119eca5ffdf5c0bb2984d7c6dc967c0',
'path': 'full/b294005510b8159f7508c5f084a5e0dbbfb63fbe.jpg',
'url': 'http://img95.699pic.com/photo/00017/0701.jpg_wh300.jpg'},
{'checksum': '7ce71cece48dcf95b86e0e5afce9985d',
'path': 'full/035017aa993bb72f495a7014403bd558f1883430.jpg',
'url': 'http://img95.699pic.com/photo/00022/2328.jpg_wh300.jpg'},
{'checksum': 'ac1d9a9569353ed92baddcedd9a0d787',
'path': 'full/71038a4d15c0e5613831d49d7a6d5901d40426ac.jpg',
'url': 'http://img95.699pic.com/photo/00019/6796.jpg_wh300.jpg'},
{'checksum': 'ad1732345aeb5534cb77bd5d9cffd847',
'path': 'full/c01104e93ed52a3d62c6a5edb2970281b2d0902b.jpg',
'url': 'http://img95.699pic.com/photo/00004/4944.jpg_wh300.jpg'},
{'checksum': 'c3c216d12719b5c00df3a85baef90aff',
'path': 'full/1470a42ad964c97867459645b14adc73c870f0e1.jpg',
'url': 'http://img95.699pic.com/photo/50016/6025.jpg_wh300.jpg'},
{'checksum': '74c37b5a6e417ecfa7151683b178e2b3',
'path': 'full/821cea60c4ee6b388b5245c6cc7e5aa5d07dacb5.jpg',
'url': 'http://img95.699pic.com/photo/00002/3437.jpg_wh300.jpg'},
{'checksum': 'f991181e76a5017769140756097b18f5',
'path': 'full/a8aa882e28f0704dd0c32d783df860f8b5617b45.jpg',
'url': 'http://img95.699pic.com/photo/00014/2406.jpg_wh300.jpg'},
{'checksum': '0878fe7552a6c1c5cfbd45ae151891c8',
'path': 'full/9c147ed85db3823b1072a761e30e8f2a517e7af0.jpg',
'url': 'http://img95.699pic.com/photo/00007/4890.jpg_wh300.jpg'}]}
2017-06-09 22:38:20 [scrapy] INFO: Closing spider (finished)
2017-06-09 22:38:20 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 10145,
'downloader/request_count': 26,
'downloader/request_method_count/GET': 26,
'downloader/response_bytes': 1612150,
'downloader/response_count': 26,
'downloader/response_status_count/200': 26,
'file_count': 25,
'file_status_count/downloaded': 25,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 6, 9, 14, 38, 20, 962000),
'item_scraped_count': 1,
'log_count/DEBUG': 53,
'log_count/INFO': 7,
'response_received_count': 26,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2017, 6, 9, 14, 38, 19, 382000)}
2017-06-09 22:38:20 [scrapy] INFO: Spider closed (finished)
从log日志中可以看到image_urls和images中分别保存了图片URL以及返回的路径。比如下面的这个例子。Jpg图片保存在full文件夹下面
full/1470a42ad964c97867459645b14adc73c870f0e1.jpg
结合之前设置的IMAGES_STORE的值,图片的完整路径应该是在E:\scrapy_project\test1\image\full。在这个路径下可以看到下载完成的图片
根据scrapy的官方文档,get_media_requests和item_completed也可以自己重写,代码如下,和系统自带的代码差不多是一样的
class Test1Pipeline(ImagesPipeline):
def get_media_requests(self,item,info):
for image_url in item['image_urls']:
yield Request(image_url)
def item_completed(self, results, item, info):
image_path=[x['path'] for ok,x in results if ok]
print image_path
if not image_path:
raise DropItem('items contains no images')
item['image_paths']=image_path
return item
首先在get_media_requests对每个图片的路径进行请求,并对各个URL图片返回一个Request,这些请求被管道处理,当它们下载完成后。结果以元组列表的形式返回到item_completed
Success 是一个布尔值,当成功下载是为True,失败为False
Image_infor_or_error 是一个包含下列关键字的字典(如果成功为True,失败为Twisted Failure)
url:图片下载的url,就是从get_media_requests中返回请求的URL
path:图片存储的路径。就像full/1ca5879492b8fd606df1964ea3c1e2f4520f076f.jpg这种格式的
checksum:图片内容的MD5 hash
item_completed需要返回一个输出,将其送到随后的项目管道阶段。因此我们将下载图片的路径存储在image_path中并返回
python网络爬虫之使用scrapy爬取图片的更多相关文章
- 【python 网络爬虫】之scrapy系列
网络爬虫之scripy系列 [scrapy网络爬虫]之0 爬虫与反扒 [scrapy网络爬虫]之一 scrapy框架简介和基础应用 [scrapy网络爬虫]之二 持久化操作 [scrapy网络爬虫]之 ...
- python 网络爬虫(一)爬取天涯论坛评论
我是一个大二的学生,也是刚接触python,接触了爬虫感觉爬虫很有趣就爬了爬天涯论坛,中途碰到了很多问题,就想把这些问题分享出来, 都是些简单的问题,希望大佬们以宽容的眼光来看一个小菜鸟
- Python网络爬虫入门实战(爬取最近7天的天气以及最高/最低气温)
_ 前言 本文文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Bo_wen 最近两天学习了一下python,并自己写了一个 ...
- python网络爬虫之使用scrapy自动爬取多个网页
前面介绍的scrapy爬虫只能爬取单个网页.如果我们想爬取多个网页.比如网上的小说该如何如何操作呢.比如下面的这样的结构.是小说的第一篇.可以点击返回目录还是下一页 对应的网页代码: 我们再看进入后面 ...
- python网络爬虫第三弹(<爬取get请求的页面数据>)
一.urllib库 urllib是python自带的一个用于爬虫的库,其主要作用就是通过代码模拟浏览器发送请求,其常被用到的子模块在 python3中的为urllib.request 和 urllib ...
- Python网络爬虫(6)--爬取淘宝模特图片
经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...
- python网络爬虫之使用scrapy自动登录网站
前面曾经介绍过requests实现自动登录的方法.这里介绍下使用scrapy如何实现自动登录.还是以csdn网站为例. Scrapy使用FormRequest来登录并递交数据给服务器.只是带有额外的f ...
- python网络爬虫之使用scrapy下载文件
前面介绍了ImagesPipeline用于下载图片,Scrapy还提供了FilesPipeline用与文件下载.和之前的ImagesPipeline一样,FilesPipeline使用时只需要通过it ...
- Python网络爬虫——Appuim+夜神模拟器爬取得到APP课程数据
一.背景介绍 随着生产力和经济社会的发展,温饱问题基本解决,人们开始追求更高层次的精神文明,开始愿意为知识和内容付费.从2016年开始,内容付费渐渐成为时尚. 罗辑思维创始人罗振宇全力打造" ...
随机推荐
- 牛客网 Wannafly挑战赛9 C.列一列-sscanf()函数
C.列一列 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 链接:https://www.now ...
- LA 2995 Image Is Everything
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- BZOJ2527Meteors
BZOJ2527 整体二分模板题 整体二分: 主要用于解决第K大问题 #include<cstdio> #include<cctype> #include<vector& ...
- Java生成GUID的方法
其实在Java上已经换了一个说法,叫做UUID,方法如下: java.util.UUID.randomUUID()
- java正则过虑字符
public static void main(String[] args) { String testrString = "{\"abc\" : \"[123 ...
- 关于查看python的trace的方法
lptrace本质上是基于GDB的,进入到进程内存空间,然后执行了一段python指令把当时的trace给print出来 使用工具:https://github.com/khamidou/lptrac ...
- [Javascript] Await a JavaScript Promise in an async Function with the await Operator
The await operator is used to wait for a promise to settle. It pauses the execution of an async func ...
- HDU4126Genghis Khan the Conqueror(最小生成树+并查集)
Genghis Khan the Conqueror Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 327680/327680 K ...
- php.ini的载入位置
php.ini文件找不到,载入WINDOS下的,但找不到,后来强制-c查找是OK的.思考,为什么载入window下的ini文件.1.可能是有一个默认路径.2.可能没有路径.默认载入. 问题解决:htt ...
- 使用Cout输出String和CString对象
CString和string都是一个类,不同的是CString主要用于MFC或者是ATL编程中,而string则多用于Windows控制台编程中 在实际编程过程中,我们经常用到string或者是CSt ...