鱼C论坛

 找回密码
 立即注册
查看: 1069|回复: 6

[已解决]新手求助 爬取煎蛋网报错 OSError

[复制链接]
发表于 2020-5-17 14:07:41 | 显示全部楼层 |阅读模式

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

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

x
  1. import re
  2. import urllib.request
  3. import os


  4. def open_url(url):  #设置访问对象
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
  7.     response = urllib.request.urlopen(req)
  8.     html = response.read().decode('utf-8')
  9.     return html


  10. def get_img(html):   #获取图片
  11.     p = r'<img src="([^"]+\.jpg")'
  12.     imglist = re.findall(p,html)
  13.     '''
  14.     for each in imglist:
  15.         print(each)
  16.            '''
  17.     for each in imglist:
  18.         filename =each.split('/')[-1]
  19.         print(filename)
  20. #报错应该在这里
  21.         pic = 'https:'+ each
  22.         with open(filename, 'wb') as f:
  23.             img = open_url(pic)
  24.             f.write(img)

  25.         
  26. if __name__ == '__main__':
  27.     url ='http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
  28.     get_img(open_url(url))
复制代码

报错如下:
  1. Traceback (most recent call last):
  2.   File "C:\Users\Administrator\Desktop\hello.py", line 32, in <module>
  3.     get_img(open_url(url))
  4.   File "C:\Users\Administrator\Desktop\hello.py", line 25, in get_img
  5.     with open(filename, 'wb') as f:
  6. OSError: [Errno 22] Invalid argument: '6e2a5d4dly1g10d7j3l6oj20j30rvq5q.jpg"'
复制代码


如果不用图片的原名称,怎么定义图片的名称,比如从1开始取值
最佳答案
2020-5-17 17:01:08
qiangqiang1 发表于 2020-5-17 16:57
下载下来的图片无法打开,是不是编码格式的问题

嗯,编码问题 在重新定义一个不用 decode()即可
  1. import re
  2. import urllib.request
  3. import os


  4. def open_url(url):  # 设置访问对象
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent',
  7.                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
  8.     response = urllib.request.urlopen(req)
  9.     html = response.read().decode('utf-8', "replace")
  10.     return html

  11. def open_url2(url):
  12.     req = urllib.request.Request(url)
  13.     req.add_header('User-Agent',
  14.                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
  15.     response = urllib.request.urlopen(req)
  16.     html = response.read()
  17.     return html


  18. def get_img(html):  # 获取图片
  19.     p = r'<img src="([^"]+\.jpg)'
  20.     imglist = re.findall(p, html)
  21.     '''
  22.     for each in imglist:
  23.         print(each)
  24.            '''
  25.     count = 0
  26.     for each in imglist:
  27.         count += 1
  28.         filename = '%d.jpg'
  29.         pic = 'http:' + each
  30.         with open(filename % count, 'wb') as f:
  31.             img = open_url2(pic)
  32.             f.write(img)


  33. if __name__ == '__main__':
  34.     url = 'http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
  35.     get_img(open_url(url))

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-17 14:23:23 | 显示全部楼层
本帖最后由 Twilight6 于 2020-5-17 14:58 编辑

你看看把代码改这样行不行的?
  1. count = 0
  2.     for each in imglist:
  3.         count += 1
  4.         filename = '%d.jpg' # 名字也是太多奇葩字符了 ,建议参照三楼所说,或者你爬个里面的专辑名?
  5.         # 这边要改成http  原因看下面截图
  6.         pic = 'http:' + each
  7.         with open(filename%count, 'wb') as f:
  8.             img = open_url(pic)
  9.             f.write(img)
复制代码

http.png

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-5-17 14:24:13 | 显示全部楼层
你只要可以下载图片,你为啥非要让打开方式为filename呢?
可以自定义一个i,然后每次写入完i就+1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-17 15:52:58 | 显示全部楼层
Twilight6 发表于 2020-5-17 14:23
你看看把代码改这样行不行的?

这样写我遇到一个新的错误如下:
  1. Traceback (most recent call last):
  2.   File "C:\Users\Administrator\Desktop\hello.py", line 34, in <module>
  3.     get_img(open_url(url))
  4.   File "C:\Users\Administrator\Desktop\hello.py", line 30, in get_img
  5.     f.write(img)
  6. TypeError: a bytes-like object is required, not 'str'
复制代码

代码:
  1. import re
  2. import urllib.request
  3. import os


  4. def open_url(url):  #设置访问对象
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
  7.     response = urllib.request.urlopen(req)
  8.     html = response.read().decode('utf-8',"replace")
  9.     return html
  10.    

  11. def get_img(html):   #获取图片
  12.     p = r'<img src="([^"]+\.jpg)'
  13.     imglist = re.findall(p,html)
  14.     '''
  15.     for each in imglist:
  16.         print(each)
  17.            '''
  18.     count = 0
  19.     for each in imglist:
  20.         count += 1
  21.         filename = '%d.jpg'
  22.         pic = 'http:' + each
  23.         with open(filename%count, 'wb') as f:
  24.             img = open_url(pic)
  25.             f.write(img)
  26.             
  27. if __name__ == '__main__':
  28.     url ='http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
  29.     get_img(open_url(url))

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-17 15:58:26 | 显示全部楼层
qiangqiang1 发表于 2020-5-17 15:52
这样写我遇到一个新的错误如下:
代码:
  1.     for each in imglist:
  2.         count += 1
  3.         filename = '%d.jpg'
  4.         pic = 'http:' + each
  5.         with open(filename % count, 'wb') as f:
  6.             img = open_url(pic)
  7.             f.write(img.encode())# 这边把 格式转回来就好
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-17 16:57:28 | 显示全部楼层

下载下来的图片无法打开,是不是编码格式的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-17 17:01:08 | 显示全部楼层    本楼为最佳答案   
qiangqiang1 发表于 2020-5-17 16:57
下载下来的图片无法打开,是不是编码格式的问题

嗯,编码问题 在重新定义一个不用 decode()即可
  1. import re
  2. import urllib.request
  3. import os


  4. def open_url(url):  # 设置访问对象
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent',
  7.                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
  8.     response = urllib.request.urlopen(req)
  9.     html = response.read().decode('utf-8', "replace")
  10.     return html

  11. def open_url2(url):
  12.     req = urllib.request.Request(url)
  13.     req.add_header('User-Agent',
  14.                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
  15.     response = urllib.request.urlopen(req)
  16.     html = response.read()
  17.     return html


  18. def get_img(html):  # 获取图片
  19.     p = r'<img src="([^"]+\.jpg)'
  20.     imglist = re.findall(p, html)
  21.     '''
  22.     for each in imglist:
  23.         print(each)
  24.            '''
  25.     count = 0
  26.     for each in imglist:
  27.         count += 1
  28.         filename = '%d.jpg'
  29.         pic = 'http:' + each
  30.         with open(filename % count, 'wb') as f:
  31.             img = open_url2(pic)
  32.             f.write(img)


  33. if __name__ == '__main__':
  34.     url = 'http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
  35.     get_img(open_url(url))

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 13:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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