Hu必成 2021-08-19 23:39 采纳率: 100%
浏览 223
已结题

python 保存文件 ,报错‘[Errno 2] No such file or directory:

本人刚刚接触python,爬了个图片网站,最后保存遇见了问题
报错’‘[Errno 2] No such file or directory: '动漫/210813/224838-16288661183b3f.jpg'
把保存图片的代码注释掉,选择在动漫文件夹里面添加文本就不报错
所以我确定我的文件路径是没有问题的

import requests
from bs4 import BeautifulSoup
import os

print(os.getcwd())
url = 'https://pic.netbian.com/4kdongman/'
resp = requests.get(url)
resp.encoding = 'gbk'

bs = BeautifulSoup(resp.text, 'html.parser')
a_list = bs.find('div', class_='slist').find_all('a')
shild_urls = []
for a in a_list:
    a = a.get('href')
    shild_url = url.split('/4kdongman/')[0] + a
    shild_urls.append(shild_url)


src_list = []
for b in shild_urls:
    resp = requests.get(b)
    resp.encoding = 'gbk'
    bs = BeautifulSoup(resp.text, 'html.parser')
    img = bs.find('div', class_='photo-pic').find('img')
    src = url.split('/4kdongman/')[0] + img.get('src')
    src_list.append(src)
for src in src_list:
    name = src.split('allimg/')[1]
    with open(f'动漫/{name}', mode='wb') as f:
        resp = requests.get(src).content
        f.write(resp)

import requests
from bs4 import BeautifulSoup
import os

print(os.getcwd())
url = 'https://pic.netbian.com/4kdongman/'
resp = requests.get(url)
resp.encoding = 'gbk'

bs = BeautifulSoup(resp.text, 'html.parser')
a_list = bs.find('div', class_='slist').find_all('a')
shild_urls = []
for a in a_list:
    a = a.get('href')
    shild_url = url.split('/4kdongman/')[0] + a
    shild_urls.append(shild_url)


src_list = []
for b in shild_urls:
    resp = requests.get(b)
    resp.encoding = 'gbk'
    bs = BeautifulSoup(resp.text, 'html.parser')
    img = bs.find('div', class_='photo-pic').find('img')
    src = url.split('/4kdongman/')[0] + img.get('src')
    src_list.append(src)
# for src in src_list:
#     name = src.split('allimg/')[1]
#     with open(f'动漫/{name}', mode='wb') as f:
#         resp = requests.get(src).content
#         f.write(resp)

f =open('动漫/03.text', mode='w')
f.write('123456')


img

  • 写回答

2条回答 默认 最新

  • Pliosauroidea 2021-08-19 23:42
    关注

    F盘里如果没有动漫这个文件夹就会报错

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月28日
  • 已采纳回答 8月20日
  • 创建了问题 8月19日

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择