Scrapy 下载图片

Posted LightSong@计海拾贝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scrapy 下载图片相关的知识,希望对你有一定的参考价值。

参考 : https://www.jianshu.com/p/6c8d2730d088

https://docs.scrapy.org/en/latest/topics/item-pipeline.html#writing-your-own-item-pipeline


import scrapy

import requests
import os


class MeinvSpider(scrapy.Spider):
     name = "get_meinv"

    start_urls = [
         ‘https://www.du114.com/‘,
     ]

    def parse(self, response):

        dir_path = ‘%s/%s‘ % (".", self.name)

        if not os.path.exists(dir_path):
             os.makedirs(dir_path)

        for imggroup in response.css(‘div.Column-picBox‘):
             imgset = imggroup.css(‘ul>li img::attr("src")‘)
             for image_url in imgset.extract():

                print("image_url=%s" % image_url)

                us = image_url.split(‘/‘)[3:]
                 image_file_name = ‘_‘.join(us)
                 file_path = ‘%s/%s‘ % (dir_path, image_file_name)

                if os.path.exists(file_path):
                     continue

                with open(file_path, ‘wb‘) as handle:
                     response = requests.get(image_url, stream=True)
                     for block in response.iter_content(1024):
                         if not block:
                             break

                        handle.write(block)

以上是关于Scrapy 下载图片的主要内容,如果未能解决你的问题,请参考以下文章

Scrapy图片下载,自定义图片名字

[Python_scrapy图片爬取下载]

使用Scrapy自带的ImagesPipeline下载图片,并对其进行分类。

爬虫框架Scrapy之案例三图片下载器

Scrapy 下载图片

十八scrapy内置媒体(图片和文件)下载方式