鱼C论坛

 找回密码
 立即注册
查看: 822|回复: 3

Python 写入时 [Errno 2] No such file or directory

[复制链接]
发表于 2019-3-6 21:22:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求各位鱼油帮帮忙,代码如下:
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from multiprocessing import Pool

  4. # 获取页面url列表
  5. def get_list(url,headers):
  6.     try:
  7.         res = requests.get(url,headers = headers)
  8.         if res.status_code == 200:
  9.             soup = BeautifulSoup(res.content,'html.parser')
  10.             lis = soup.find('div',class_='tab_box').find_all('li')
  11.             for i in lis:
  12.                 li = i.find('a').get('href')
  13.                 into_url(li,headers)
  14.     except Exception as e:
  15.         print('获取链接失败:%s'%e)


  16. # 进入页面
  17. def into_url(url,headers):
  18.     try:
  19.         res = requests.get(url,headers = headers)
  20.         if res.status_code == 200:
  21.             soup = BeautifulSoup(res.content,'html.parser')
  22.             imgurls = soup.find('ul',class_='scroll-img clearfix').find_all('li')
  23.             for i in imgurls:
  24.                 imgurl = i.find('a').get('href')
  25.                 response = requests.get(imgurl,headers = headers)
  26.                 soups = BeautifulSoup(response.content,'html.parser')
  27.                 downimg = soups.find('img',class_='pic-large').get('src')
  28.                 title = soups.find('div',class_='ptitle').text
  29.                 download(downimg,title,headers)
  30.     except Exception as e:
  31.         print('进入图册失败:%s'%e)


  32. # 下载函数
  33. def download(url,title,headers):
  34.     print('获取图片链接成功:%s'%url)
  35.     try:
  36.         img = requests.get(url,headers = headers).content
  37.         with open(title + '.jpg','wb') as f:
  38.             f.write(img)
  39.     except Exception as e:
  40.         print('下载失败:%s'%e)


  41. #主函数
  42. def main(num):
  43.     headers = {'accept-encoding' : 'gzip, deflate, br','accept-language' : 'zh-CN,zh;q=0.9','cookie' : '__cfduid=d785b1a05ce079fc4b377ba844ad7db101551847363; PHPSESSID=doob179fokrompnom6d574fod7','user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}
  44.     url = 'http://www.win4000.com/zt/meinv_' + str(num) + '.html'
  45.     get_list(url,headers)

  46.    
  47. if __name__ == '__main__':
  48.     pool = Pool()
  49.     pool.map(main,[i for i in range(1,6)])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-6 22:15:00 | 显示全部楼层
没路径,你的目录没创建就写入文件就会报出这种错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-6 22:35:14 From FishC Mobile | 显示全部楼层
os.mkdir
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-7 19:56:31 From FishC Mobile | 显示全部楼层
_谪仙 发表于 2019-3-6 22:15
没路径,你的目录没创建就写入文件就会报出这种错误

但是,我也没有引入目录啊!我是直接写入的啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-14 08:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表