马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import urllib.request
import os
import time
import random
def url_open(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/69.0.3947.100 Safari/537.36')
# req.add_header('Referer', url)#设置请求头,如果爬下来的都是防盗图请参考网站最新请求头更改
'''iplist =['23.244.28.27:3128', '65.49.222.187:8080', '167.99.236.14:80']
proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)'''#代理
response = urllib.request.urlopen(req)
html = response.read()
return html
def get_page(url): #得到图片页数
html = url_open(url).decode('utf-8')
a = html.find('page-numbers current') + 22
b = html.find('<', a)
print('已获取到当前所在页面!')
return html[a:b]
#捕捉图片地址
def find_imgs(url):
html = url_open(url).decode('utf-8')
img_addrs = []
a = html.find("data-original=")
while a != -1:
b = html.find('.jpg', a, a+255)
if b != -1:
img_addrs.append(html[a+15: b+4])#.jpg是四个字符
else:
b = a + 15 #b找不到,b的地址改变
a = html.find("data-original=", b) #下一次的起始位置是上一次的结束位置
print('成功获取%d张图片地址!' % len(img_addrs))
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)
# time.sleep(5)
def download_mm(folder='OOXX',pages=2): #主函数,生成文件夹,将图片保存在文件夹里
os.mkdir(folder)
os.chdir(folder)
url = "https://www.mzitu.com/"
page_num = int(get_page(url))
for i in range(pages):
page_num +=i
page_url = url + 'page/' + str(page_num)+'/'
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)
if __name__ =='__main__':
download_mm()
|