import time
import requests
import re
import os
from bs4 import BeautifulSoup
from requests.exceptions import ConnectionError, ReadTimeout
url = 'https://www.meitulu.com/rihan/'
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36',
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Host': 'm.meitulu.com',
"Referer": "https://mtl.gzhuibei.com/"
}
headers2 = {'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36'
}
def getURL(url):
a = requests.get(url,headers=headers)
a.encoding = 'utf-8'
html = a.text
return html
def doDown():
soup = BeautifulSoup(getURL(url), 'html.parser')
for i in range(1, 95):
mu_lu_url = "https://www.meitulu.com/rihan/" + str(i) + ".html"
soup2 = BeautifulSoup(getURL(mu_lu_url), 'html.parser')
img_mu_lu = soup2.find("ul",{"class":"img"}).find_all("li")
for mu_lu in img_mu_lu:
result = {
'title': mu_lu.find("img")['alt'],
'link': mu_lu.find("img")['src']
}
print('图集名称为:', result['title'],',图片地址为:',result['link'])
img_title = result['title'][-5:]
start1 = img_title.find('[')
end1 = img_title.rfind(']')
img_count = img_title[start1 + 1:end1]
mkdir('C:/Users/25308/Desktop/Python/美图录/' + result['title'])
try:
for j in range(1, int(img_count)):
start2 = result['link'].find('img/')
end2 = result['link'].rfind('/0.jpg')
img_url = result['link'][start2 + 4:end2]
real_img_url = 'https://mtl.gzhuibei.com/images/img/' + img_url + '/' + str(j) + '.jpg'
downImage(j,real_img_url)
time.sleep(yanshi)
except Exception as ex:
print("出现如下异常%s"%ex)
def downImage(name,image):
print('第', name, '张图片', ',图片实际地址为:', image)
f = open(str(name) + '.jpg', 'wb+')
img = requests.get(image, headers=headers2)
print('请求返回结果:',img)
if str(img) == '':
print('下载图片...', end='')
f.write(img.content)
f.close()
def mkdir(name):
if os.path.exists(name):
print('文件已经存在,不需要创建!')
os.chdir(name)
else:
print('创建文件夹:')
os.mkdir(name)
os.chdir(name)
if __name__ == '__main__':
mkdir('C:/Users/25308/Desktop/Python/美图录/')
yanshi = 0.5
doDown()