新手求助 爬取煎蛋网报错 OSError
import reimport urllib.request
import os
def open_url(url):#设置访问对象
req = urllib.request.Request(url)
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')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
return html
def get_img(html): #获取图片
p = r'<img src="([^"]+\.jpg")'
imglist = re.findall(p,html)
'''
for each in imglist:
print(each)
'''
for each in imglist:
filename =each.split('/')[-1]
print(filename)
#报错应该在这里
pic = 'https:'+ each
with open(filename, 'wb') as f:
img = open_url(pic)
f.write(img)
if __name__ == '__main__':
url ='http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
get_img(open_url(url))
报错如下:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\hello.py", line 32, in <module>
get_img(open_url(url))
File "C:\Users\Administrator\Desktop\hello.py", line 25, in get_img
with open(filename, 'wb') as f:
OSError: Invalid argument: '6e2a5d4dly1g10d7j3l6oj20j30rvq5q.jpg"'
如果不用图片的原名称,怎么定义图片的名称,比如从1开始取值
本帖最后由 Twilight6 于 2020-5-17 14:58 编辑
你看看把代码改这样行不行的?
count = 0
for each in imglist:
count += 1
filename = '%d.jpg' # 名字也是太多奇葩字符了 ,建议参照三楼所说,或者你爬个里面的专辑名?
# 这边要改成http原因看下面截图
pic = 'http:' + each
with open(filename%count, 'wb') as f:
img = open_url(pic)
f.write(img)
你只要可以下载图片,你为啥非要让打开方式为filename呢?
可以自定义一个i,然后每次写入完i就+1 Twilight6 发表于 2020-5-17 14:23
你看看把代码改这样行不行的?
这样写我遇到一个新的错误如下:Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\hello.py", line 34, in <module>
get_img(open_url(url))
File "C:\Users\Administrator\Desktop\hello.py", line 30, in get_img
f.write(img)
TypeError: a bytes-like object is required, not 'str'
代码:import re
import urllib.request
import os
def open_url(url):#设置访问对象
req = urllib.request.Request(url)
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')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8',"replace")
return html
def get_img(html): #获取图片
p = r'<img src="([^"]+\.jpg)'
imglist = re.findall(p,html)
'''
for each in imglist:
print(each)
'''
count = 0
for each in imglist:
count += 1
filename = '%d.jpg'
pic = 'http:' + each
with open(filename%count, 'wb') as f:
img = open_url(pic)
f.write(img)
if __name__ == '__main__':
url ='http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
get_img(open_url(url))
qiangqiang1 发表于 2020-5-17 15:52
这样写我遇到一个新的错误如下:
代码:
for each in imglist:
count += 1
filename = '%d.jpg'
pic = 'http:' + each
with open(filename % count, 'wb') as f:
img = open_url(pic)
f.write(img.encode())# 这边把 格式转回来就好 Twilight6 发表于 2020-5-17 15:58
下载下来的图片无法打开,是不是编码格式的问题 qiangqiang1 发表于 2020-5-17 16:57
下载下来的图片无法打开,是不是编码格式的问题
嗯,编码问题 在重新定义一个不用 decode()即可
import re
import urllib.request
import os
def open_url(url):# 设置访问对象
req = urllib.request.Request(url)
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')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8', "replace")
return html
def open_url2(url):
req = urllib.request.Request(url)
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')
response = urllib.request.urlopen(req)
html = response.read()
return html
def get_img(html):# 获取图片
p = r'<img src="([^"]+\.jpg)'
imglist = re.findall(p, html)
'''
for each in imglist:
print(each)
'''
count = 0
for each in imglist:
count += 1
filename = '%d.jpg'
pic = 'http:' + each
with open(filename % count, 'wb') as f:
img = open_url2(pic)
f.write(img)
if __name__ == '__main__':
url = 'http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
get_img(open_url(url))
页:
[1]