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.sogoucdn.com/.*)" 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]
urllib.request.urlretrieve(each,filename,None)
if __name__ == "__main__":
url = "https://pic.sogou.com/pics?query=%B2%CC%D0%EC%C0%A4%CD%BC%C6%AC&ie=gbk&p=40230500&st=255&mode=255&policyType=0"
get_img(open_url(url))
为什么下载不了图片呢·? 请问·哪里有错误吗?