码迷,mamicode.com
首页 > 其他好文 > 详细

Scrapy 下载图片

时间:2018-04-07 22:49:32      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:cbo   参考   下载   os.path   col   ref   extra   requests   dir   

参考 : 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 下载图片

标签:cbo   参考   下载   os.path   col   ref   extra   requests   dir   

原文地址:https://www.cnblogs.com/lightsong/p/8734826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!