daobalong 2021-09-04 11:53 采纳率: 80%
浏览 182
已结题

python 爬取图片,部分图片无法下载,求解答

import requests
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
    'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding' : 'gzip',
    'DNT' : '1',
    'Connection' : 'close',
    'Accept-Language':'zh-CN'
}

#a存储地址
a = 'F:/CodeWar/spider/goood-download-test-20210903-b/长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/015-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd.jpg'

#b存储名称为下载地址的简单处理(在名字末端加入‘-缩略图’)
b = 'F:/CodeWar/spider/goood-download-test-20210903-b/长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/202012000-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd-472x303-缩略图.jpg'

#c存储地址
c = 'F:/CodeWar/spider/goood-download-test-20210903-b/长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/002-Sichuan-Dayi-Country-Ecology-Museum-Moire-Pattern-Dynamic-Wall-China-by-Institute-of-Architectural-Algorithms-and-Applications-Southeast-University-Architectural-Design-and-Research-Institute-Co-Ltd.jpg'


#图片下载地址
address_a = 'https://oss.gooood.cn/uploads/2020/12/009-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd.jpg'

address_b = 'https://oss.gooood.cn/uploads/2020/12/000-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd-472x303.jpg'

address_c = 'https://oss.gooood.cn/uploads/2020/12/002-Sichuan-Dayi-Country-Ecology-Museum-Moire-Pattern-Dynamic-Wall-China-by-Institute-of-Architectural-Algorithms-and-Applications-Southeast-University-Architectural-Design-and-Research-Institute-Co-Ltd.jpg'


imageA = requests.get(address_a, headers=headers)
with open(a, 'wb') as f:
    f.write(imageA.content)

imageB = requests.get(address_b, headers=headers)
with open(b, 'wb') as f:
    f.write(imageB.content)

imageC = requests.get(address_c, headers=headers)
with open(c, 'wb') as f:
    f.write(imageC.content)

图片b和图片c都无法下载,报错提示:

Traceback (most recent call last):
  File "c:/Users/long/Desktop/临时资料处理处/xiazai-20210903.py", line 37, in <module>
    with open(b, 'wb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'F:/CodeWar/spider/goood-download-test-20210903-b/长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/202012000-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd-472x303-缩略图.jpg'

试过一些方法,比如存储路径用的绝对路径,都行不通,求解答

  • 写回答

9条回答 默认 最新

  • 急速光粒 2021-09-04 13:19
    关注

    你的目录名过长,而且多级目录,是不是有点自己找苦吃,你确保这个目录存在吗?建议你简化目录结构并确保目录存在,如再不成功再发问题。有帮助请采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • Jason Ho 2021-09-04 11:56
    关注

    用 you-get

    评论
  • Pliosauroidea 2021-09-04 11:57
    关注

    能否发一下完整报错截图,
    从如上报错来看是没找到对应文件夹
    检测一下第一张图片是否成功下载,删除本地下载到的第一张图片重新跑一下试试有没有新报错,截图发一下上来

    评论
  • Pliosauroidea 2021-09-04 12:00
    关注
    import requests
    import os,sys
    os.chdir(sys.path[0])
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
        'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Encoding' : 'gzip',
        'DNT' : '1',
        'Connection' : 'close',
        'Accept-Language':'zh-CN'
    }
    if not os.path.exists('./长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1'):
        os.mkdir('长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1')
    #a存储地址
    a = '长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/015-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd.jpg'
    #b存储名称为下载地址的简单处理(在名字末端加入‘-缩略图’)
    b = '长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/202012000-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd-472x303-缩略图.jpg'
    #c存储地址
    c = '长春中医药大学附属第三临床医院,吉林 - 清华大学建筑设计研究院有限公司1/002-Sichuan-Dayi-Country-Ecology-Museum-Moire-Pattern-Dynamic-Wall-China-by-Institute-of-Architectural-Algorithms-and-Applications-Southeast-University-Architectural-Design-and-Research-Institute-Co-Ltd.jpg'
     
    #图片下载地址
    address_a = 'https://oss.gooood.cn/uploads/2020/12/009-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd.jpg'
    address_b = 'https://oss.gooood.cn/uploads/2020/12/000-The-Third-Affiliated-Hospital-of-Changchun-University-of-Chinese-Medicine-China-by-Architectural-Design-and-Research-Institute-of-Tsinghua-University-Co-Ltd-472x303.jpg'
    address_c = 'https://oss.gooood.cn/uploads/2020/12/002-Sichuan-Dayi-Country-Ecology-Museum-Moire-Pattern-Dynamic-Wall-China-by-Institute-of-Architectural-Algorithms-and-Applications-Southeast-University-Architectural-Design-and-Research-Institute-Co-Ltd.jpg'
     
    imageA = requests.get(address_a, headers=headers)
    with open(a, 'wb') as f:
        f.write(imageA.content)
    imageB = requests.get(address_b, headers=headers)
    with open(b, 'wb') as f:
        f.write(imageB.content)
    imageC = requests.get(address_c, headers=headers)
    with open(c, 'wb') as f:
        f.write(imageC.content)
    

    用相对路径修改了一下,运行的时候会在同目录下新建文件夹并存入

    img


    有帮助望采纳

    评论
  • 快ྂ乐ྂ至ྂ上ྂ 2021-09-04 12:04
    关注

    你得问题,能不能截图完整

    评论
  • TGananu 2021-09-04 12:56
    关注
    #a存储地址
    a = 'C:/Users/TGananu/Desktop/下载/1.jpg'
    #b存储名称为下载地址的简单处理(在名字末端加入‘-缩略图’)
    b = 'C:/Users/TGananu/Desktop/下载/2.jpg'
    #c存储地址
    c = 'C:/Users/TGananu/Desktop/下载/3.jpg'
    

    确保存储地址下有文件
    如:a = 'C:/Users/TGananu/Desktop/下载/1.jpg'
    先在目录 C:/Users/TGananu/Desktop/下载/ 下创建好jpg文件

    评论
  • 鸡蛋酱$ 2021-09-04 13:04
    关注

    在你用的字符串前面加一个r,你可以新建一个文件夹,os.chdir("路径"),全部下载到这个新的文件夹,然后再os.rename到你指定的文件夹,有用的话点一下采纳

    评论
  • Mint.Coder 2021-09-04 17:01
    关注

    应该是文件名问题,把那些符号去掉。

    评论
  • 孙叫兽 前端领域优质创作者 2021-09-04 12:29
    关注

    出现这种情况,首先查看目录是否有错,该目录文件是否存在。

    其次在Python中“\”会被自动认为是转义字符,而非目录分隔符

    评论
查看更多回答(8条)

报告相同问题?

问题事件

  • 系统已结题 9月12日
  • 已采纳回答 9月4日
  • 修改了问题 9月4日
  • 创建了问题 9月4日

悬赏问题

  • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
  • ¥30 各位help写一下代码
  • ¥15 在运行SDEdit模型下载不了
  • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
  • ¥15 电脑连上WIFI却用不了
  • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
  • ¥15 hyper-v出现的问题
  • ¥15 有能用的可加酬金,求可以批量下载懒人听书的软件,能登录自己帐号的。
  • ¥100 高博一起做RGB-D SLAM(5)VO无法出visualisation问题
  • ¥15 使用matlab进行手眼标定的仿真验证,得到齐次矩阵与opencv相差较大