#2
fall_bernana2020-08-31 10: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/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0")
page = urllib.request.urlopen(req)
html = page.read().decode("utf-8")
return html
def get_img(html):
p = (r'<img src="(http[^_]*//i0\dpiccdn.*)" data-img="" style="height:214.1px;width:[^a-z]*px;".*">')
imglist = re.findall(p,html)
try:
os.mkdir("蔡徐坤")
except FileExistsError:
pass
os.chdir("蔡徐坤")
for each in imglist:
filename = each.spilt("//")[1]
with open (filename,"wb") as f:
f.write(each)
# urllib.request.urlretrieve(each,filename,None)
if __name__ == "__main__":
url = "https://pic. get_img(open_url(url))
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/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0")
page = urllib.request.urlopen(req)
html = page.read().decode("utf-8")
return html
def get_img(html):
p = (r'<img src="(http[^_]*//i0\dpiccdn.*)" data-img="" style="height:214.1px;width:[^a-z]*px;".*">')
imglist = re.findall(p,html)
try:
os.mkdir("蔡徐坤")
except FileExistsError:
pass
os.chdir("蔡徐坤")
for each in imglist:
filename = each.spilt("//")[1]
with open (filename,"wb") as f:
f.write(each)
# urllib.request.urlretrieve(each,filename,None)
if __name__ == "__main__":
url = "https://pic. get_img(open_url(url))
能够运行没有报错,但是生成的文件夹中没有下载的图片,请问这是哪里有错误吗?
[此贴子已经被作者于2020-8-29 10:37编辑过]