|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如题
代码如下
求大神帮忙看下啦,谢谢!!
import os
import urllib.request
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def url_open(url):
'获取网页'
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read()
return html
def find_imgs(url):
'寻找图片链接位置'
html = url_open(url).decode('utf-8')
img_addrs = []
a = html.find('img')
while a != -1:
b = html.find('.jpg',a,a+255)
if b!=-1:
img_addrs.append(html[a+21:b+4])
else:
b = a+9
a = html.find('img',b)
return img_addrs
def save_imgs(folder,img_addrs):
'保存图片'
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb') as f:
img = url_open(each)
f.write(img)
def getpic(folder = '照片',pages = 10):
'主程序'
ssl._create_default_https_context = ssl._create_unverified_context
os.mkdir(folder)
os.chdir(folder)
url = 'https://www.mzitu.com/xinggan/'
for i in range(pages):
page_num =i+1
page_url = 'https://www.mzitu.com/xinggan/page/'+str(page_num)+'/'
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)
if __name__ == '__main__':
getpic()
ssl应该是针对某些要求证书认证的网站 和你说的https协议 有啥关系
|
|