爬虫下载的问题,求助!
这是一个爬取煎蛋网的代码,返回的图片地址都是没问题的,可是在下载的时候出了问题,求助!!!import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
def get_img(html):
p = re.compile('<br /><img\ssrc=(.*?)\sreferrerPolicy')
#p = re.compile('<img\ssrc="(.*?\.jpg)"')
img_list = re.findall(p,html)
save_img(img_list)
def save_img(img_list):
for each in img_list:
#print(each)
filename = each.split("/")[-1]
with open(filename, 'wb') as f:
img = open_url(each)
f.write(img)
if __name__ == '__main__':
url = 'http://jandan.net/ooxx'
os.mkdir('ooxx')
os.chdir('ooxx')
get_img(open_url(url))
这个是报错
Traceback (most recent call last):
File "C:\Users\巴鲁斯\Desktop\新建文件夹\mmtu.py", line 34, in <module>
get_img(open_url(url))
File "C:\Users\巴鲁斯\Desktop\新建文件夹\mmtu.py", line 19, in get_img
save_img(img_list)
File "C:\Users\巴鲁斯\Desktop\新建文件夹\mmtu.py", line 25, in save_img
with open(filename, 'wb') as f:
OSError: Invalid argument: '0076BSS5ly1gh25iz10zcj30u0190gra.jpg"'
>>> \ 改成 / 试试 Hello. 发表于 2020-7-24 17:03
\ 改成 / 试试
不行啊
import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
def get_img(html):
p = re.compile('<br /><img\ssrc=(.*?)\sreferrerPolicy')
#p = re.compile('<img\ssrc="(.*?\.jpg)"')
img_list = re.findall(p,html)
save_img(img_list)
def save_img(img_list):
for each in img_list:
#print(each)
filename = each.split("/")[-1]
with open(filename, 'wb') as f:
img = open_url(each)
f.write(img)
if __name__ == '__main__':
url = 'http://jandan.net/ooxx'
os.mkdir('ooxx')
os.chdir('ooxx')
get_img(open_url(url)) 大地郡主 发表于 2020-7-24 17:12
不行啊
import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
def get_img(html):
p = re.compile('<br /><img\ssrc=(.*?)\sreferrerPolicy')
#p = re.compile('<img\ssrc="(.*?\.jpg)"')
img_list = re.findall(p,html)
save_img(img_list)
def save_img(img_list):
for each in img_list:
#print(each)
filename = each.split("/")[-1]
with open(filename, 'wb') as f:
img = open_url(each)
f.write(img)
if __name__ == '__main__':
url = 'http://jandan.net/ooxx'
os.mkdir('ooxx')
os.chdir('ooxx')
get_img(open_url(url))
里面的with open(filename, 'wb') as f:里的filename换成绝对路径,不要只是文件名 大地郡主 发表于 2020-7-24 17:12
不行啊
with open(filename, 'wb') as f:里的filename换成绝对路径,不要只写个文件名
你的文件名尽量不要用一堆乱数字来命名吧,容易出错,for 循环外面 放个 count ,里面放 count + 1 ,然后文件名用 filename = str(count)+".jpg"
还有你爬虫爬取的 url 是不带 http:// 的,你应该将 img = open_url(each) 改成 img = open_url('http://'+each)
大地郡主 发表于 2020-7-24 17:12
不行啊
代码有好几处出错
首先获取到的filename带有引号
其次, img = open_url(each)这句里面的each不是一个完整链接 Twilight6 发表于 2020-7-24 17:44
你的文件名尽量不要用一堆乱数字来命名吧,容易出错,for 循环外面 放个 count ,里面放 count + 1 , ...
不行啊,改了之后存进了一张无法打开的图片,但是报错更多了
import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
def get_img(html):
p = re.compile('<br /><img\ssrc=(.*?)\sreferrerPolicy')
#p = re.compile('<img\ssrc="(.*?\.jpg)"')
img_list = re.findall(p,html)
save_img(img_list)
def save_img(img_list):
count = 0
for each in img_list:
#print(each)
count += 1
filename = str(count)+".jpg"
with open(filename, 'wb') as f:
img = open_url('http://'+each)
f.write(img)
if __name__ == '__main__':
url = 'http://jandan.net/ooxx'
os.mkdir('ooxx')
os.chdir('ooxx')
get_img(open_url(url))
1q23w31 发表于 2020-7-24 17:56
代码有好几处出错
首先获取到的filename带有引号
其次, img = open_url(each)这句里面的each不是一个 ...
好的,我再改下 大地郡主 发表于 2020-7-24 17:58
好的,我再改下
帮你改完了:
import urllib.request
import re
import os
def open_url(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
req = urllib.request.Request(url,headers=headers)
try:
page = urllib.request.urlopen(req)
html = page.read()
except:
html = ''
return html
def get_img(html):
p = re.compile("<br /><img\ssrc=(.*?)\sreferrerPolicy")
# p = re.compile('<img\ssrc="(.*?\.jpg)"')
img_list = re.findall(p, html.decode('utf-8'))
save_img(img_list)
def save_img(img_list):
count = 0
for each in img_list:
count += 1
filename = str(count) + ".jpg"
with open(filename, 'wb') as f:
img = open_url("http:" +each)
if img == '':
continue
f.write(img)
if __name__ == '__main__':
url = r"http://jandan.net/ooxx"
os.mkdir('ooxx')
os.chdir('ooxx')
get_img(open_url(url)) Twilight6 发表于 2020-7-24 18:14
帮你改完了:
学到了很多,谢谢您!
页:
[1]