[Python_scrapy图片爬取下载]

welcome to myblog

Dome地址

爬取某个车站的图片


item.py 中

1、申明item 的fields

class PhotoItem(scrapy.Item):
     # define the fields for your item here like:
     # name = scrapy.Field()
     image_urls = scrapy.Field()
     images = scrapy.Field()
     pass

spider 的image.py

  1. 导入头文件

    from Photo.items import PhotoItem
    
    from scrapy import Spider
    
    from scrapy import Selector
    
    from scrapy.http import Request
    
  2. 爬取代码

注:

只需要爬取图片对应的url

翻页爬取时加上爬取范围url

class imageSpider(Spider):
      name = 'car'
      allowed_domains = ['car.autohome.com.cn']
      start_urls = [
          "https://car.autohome.com.cn/jingxuan/list-0-p1.html",
      ]

def parse(self, response):
    item = PhotoItem()
    sel = Selector(response)
    item['image_urls'] = sel.xpath('//ul[@class="content"]/li/a/img/@src').extract()

    print item['image_urls'], '..image_urls..'
    yield item

    # 翻页
    new_urls = response.xpath('//div[@class="pageindex"]/a[9]/@href').extract_first()

    new_url = "https://car.autohome.com.cn" + new_urls

    print new_url, '..new_url...'
    if new_url:

       yield Request(new_url, callback=self.parse)

settings.py 中

大专栏  [Python_scrapy图片爬取下载]Configure item pipelines
ITEM_PIPELINES = {
    'Photo.pipelines.jandanPipeline': 200,
    # 'Photo.pipelines.PhotoPipeline': 300,
 }
存储下载图片所在位置
IMAGES_STORE = '/Users/sansi/Desktop/Scrapy/Photo/Image'
DOWNLOAD_DELAY = 0.25
缩略图大小
IMAGES_THUMBS = {
   'small': (50, 50),
   'big': (200, 200),
}
图片的失效期限
IMAGES_EXPIRES = 90

pipelines.py 中

  1. 导入头文件

    import os
    import urllib
    import scrapy
    from scrapy.exceptions import DropItem
    from scrapy.pipelines.images import ImagesPipeline
    from Photo import settings
    
  2. 编写爬取下载
    class PhotoPipeline(object):
     def process_item(self, item, spider):
         return item
         
重写ImagesPipeline,对各个url返回Request
class jandanPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield scrapy.Request(image_url)
当一个项目所有的请求完成时调用
def item_completed(self, results, item, info):
    image_paths = [x['path'] for ok, x in results if ok]
    if not image_paths:
        raise DropItem("Item contains no images")
    item['images'] = image_paths
    return item

原文地址:https://www.cnblogs.com/lijianming180/p/12284377.html

时间: 2024-08-10 14:13:10

[Python_scrapy图片爬取下载]的相关文章

回车桌面图片爬取

回车桌面图片爬取 今天我们就来爬爬这个网站 https://tu.enterdesk.com/ 这个网站能爬的资源还是很多的,但我就写一个例子,其他的可以根据思路去写. 首先还是先来分析下这个网站的图片获取过程 我选择的是图库,先随便选择一个标签,我这选宠物吧 哟,我们再看看有没有翻页 开启F12(开发者工具) 用不习惯火狐,还是开谷歌来看吧 那么就访问看看?随便选取一个访问看看是不是能出图片 https://tu.enterdesk.com/chongwu/6.html 结果肯定是可以的啦 问

Python爬虫新手教程: 知乎文章图片爬取器

1. 知乎文章图片爬取器之二博客背景 昨天写了知乎文章图片爬取器的一部分代码,针对知乎问题的答案json进行了数据抓取,博客中出现了部分写死的内容,今天把那部分信息调整完毕,并且将图片下载完善到代码中去. 首先,需要获取任意知乎的问题,只需要你输入问题的ID,就可以获取相关的页面信息,比如最重要的合计有多少人回答问题.问题ID为如下标红数字 编写代码,下面的代码用来检测用户输入的是否是正确的ID,并且通过拼接URL去获取该问题下面合计有多少答案. import requests import r

爬虫07 /scrapy图片爬取、中间件、selenium在scrapy中的应用、CrawlSpider、分布式、增量式

目录 爬虫07 /scrapy图片爬取.中间件.selenium在scrapy中的应用.CrawlSpider.分布式.增量式 1. scrapy图片的爬取/基于管道类实现 2. 中间件的使用 3. selenium在scrapy中的应用 4. CrawlSpider 5. 分布式 5. 增量式 爬虫07 /scrapy图片爬取.中间件.selenium在scrapy中的应用.CrawlSpider.分布式.增量式 1. scrapy图片的爬取/基于管道类实现 爬取流程: 爬虫类中将解析到的图片

千图网_性感美女图片爬取--图片懒加载

#爬取千图网性感美女模块的图片 #第一页:http://sc.chinaz.com/tupian/xingganmeinvtupian.html #第二页:http://sc.chinaz.com/tupian/xingganmeinvtupian_2.html #两种url结构不同,注意 可以使用if语句判断 import urllib.request import urllib.parse from lxml import etree import time import os #定义下载图

知乎高颜值图片爬取

.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > .katex-html { display: block; } .katex-display > .katex > .katex-html > .tag { position: absolute; right: 0px; } .katex { font-style: normal; font

Python爬虫学习教程,批量爬取下载抖音视频

这篇文章主要为大家详细介绍了python批量爬取下载抖音视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 这篇文章主要为大家详细介绍了python批量爬取下载抖音视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 这篇文章主要为大家详细介绍了python批量爬取下载抖音视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 项目源码展示: 1 ''' 2 在学习过程中有什么不懂得可以加我的 3 python学习交流扣扣qun,934109170 4 群里有不错的学习教程.开发工具与电子

scrapy之360图片爬取

#今日目标 **scrapy之360图片爬取** 今天要爬取的是360美女图片,首先分析页面得知网页是动态加载,故需要先找到网页链接规律, 然后调用ImagesPipeline类实现图片爬取 *代码实现* so.py ``` # -*- coding: utf-8 -*- import scrapy import json from ..items import SoItem class SoSpider(scrapy.Spider): name = 'so' allowed_domains =

糗图-图片爬取

糗图-图片爬取 主要思路 1.来到首页,查看主页有用图片存在html的规律 2.编写re提取图片路径 3.右键图片查看请求图片的具体路径 4.拼接图片请求路径 5.查看下一页界面的路径,找到界面请求路径规律 6.work,多界面爬取指定图片爬虫 import requests import re import os headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.3

python爬虫学习--pixiv爬虫(2)--国际排行榜的图片爬取

之前用面向过程的形式写了一下pixiv爬虫的登录... 觉得还是面向对象好一些... 那就先把登录过程重写一下... class Pixiv_Spider: def __init__(self): self.p_id = '' self.p_pw = '' def Login(self): #处理登录所需要的请求信息 p_login_url = 'https://www.pixiv.net/login.php' data = { #登录所要post的信息 'mode':'login', 'ski