[Python] 纯文本查看 复制代码
import requests
import re
import os
#类型列表
Type_list = {1:'qingchun',2:'xiaohua',3:'chemo',4:'qipao',5:'mingxing',6:'xinggan',}
Type_list_cn = {'qingchun':'青春美眉','xiaohua':'美女校花','chemo':'性感车模','qipao':'旗袍美女','mingxing':'明星写真','xinggan':'性感美女'}
#请求头
hd = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
#主函数
def main():
try:
for i in range(1,7):
#输出类型序号和名字 还有页数
print(f"{i}.{Type_list_cn[Type_list[i]]} 共{get_TotalPage(Type_list[i])}页")
#限制输入
while(True):
Type_num = input('输入要获取的美女类型:')
if Type_num.isdigit():
Type_num = eval(Type_num)
if Type_num in range(1,7):
break
else:
print('请输入1-6的数字!')
else:
print("请输入数字!")
#类型
Type = Type_list[Type_num]
#总页数
TotalPage = get_TotalPage(Type)
#限制输入
while(True):
DownloadPage = input("请输入要获取前多少页(为空则全部):")
if DownloadPage == '':
DownloadPage = TotalPage
break
elif DownloadPage.isdigit():
DownloadPage = eval(DownloadPage)
if DownloadPage in range(1,TotalPage+1):
break
else:
print(f"请输入1-{TotalPage}的数字!")
else:
print("请输入数字!")
pic(Type_num,Type,DownloadPage)
except:
print("程序出错!\n")
#获取总页数
def get_TotalPage(Type):
TotalPage = 0
url = 'https://www.mm131.net/'+Type
response = requests.get(url,headers = hd)
response.encoding = 'gb2312'
TotalPage_pattern = re.compile('(?<=下一页</a><a href=\'list_)\d_\d+')
#正则匹配总页数
match = (TotalPage_pattern.findall(response.text))
TotalPage = eval(match[0].split('_')[1])
return TotalPage
#下载图片总功能
def pic(Type_num,Type,DownloadPage):
print("\n正在处理第1页")
url = 'https://www.mm131.net/' + Type
get_web(url,Type)
if DownloadPage > 1:
for page in range(2,DownloadPage+1):
print("\n正在处理第{}页".format(page))
#拼凑当前页url
url = 'https://www.mm131.net/' + Type + '/list_' + str(Type_num) +'_'+ str(page) + '.html'
get_web(url,Type)
#获取当前页网页信息用以获取套图数量和套图num
def get_web(url,Type):
response = requests.get(url,headers = hd)
response.encoding = 'gb2312'
text_pattern = re.compile('您的位置[\s\S]*?末页')
#先正则匹配一次缩小范围
text = text_pattern.findall(response.text)
PicList_pattern = re.compile(f'(?<=https://www.mm131.net/{Type}/)\d+')
#再用正则匹配出套图NUM
PicList = PicList_pattern.findall(text[0])
total = len(PicList)
print(f"这页有{total}个套图\n")
for i in range(1,total+1):
pic_num = PicList[i-1]
print(f" 正在处理第{i}个套图:")
#拼凑套图页面URL
pic_url = 'https://www.mm131.net/' + Type + '/' + pic_num + '.html'
get_pic(pic_url,pic_num,Type)
#获取套图页面信息用以获取套图名字和图片数量
def get_pic(pic_url,pic_num,Type):
response = requests.get(pic_url,headers = hd)
response.encoding = 'gb2312'
pic_name_pattern = re.compile("(?<=<h5>).+(?=</h5>)")
#套图名字
pic_name = pic_name_pattern.findall(response.text)[0]
print(" 套图名字:"+pic_name)
TotalPic_pattern = re.compile('(?<=共)\d+')
#图片数量
TotalPic = eval(TotalPic_pattern.findall(response.text)[0])
print(" 共{}张图片".format(TotalPic))
#判断当前路径下是否存在相应目录,没有则创建
if os.path.exists(f'图片/{Type_list_cn[Type]}/{pic_name}') == False:
os.makedirs(f'图片/{Type_list_cn[Type]}/{pic_name}')
#开始遍历下载图片
for i in range(1,TotalPic+1):
#文本进度条
print("\r 正在下载第{:>2}张图片{:>3}%[".format(i,round(i*100/(TotalPic),1))+"$"*i+"-"*(TotalPic-i)+"]",end='')
#第1个图片的referer和后面图片不一样
if i == 1:
referer = f'https://www.mm131.net/{Type}/{pic_num}.html'
else:
referer = f'https://www.mm131.net/{Type}/{pic_num}_{i}.html'
#拼凑图片URL
url = f'https://img1.mmmw.net/pic/{pic_num}/{i}.jpg'
download_pic(i,url,pic_name,Type,referer)
else:
print(" 套图已存在!",end = '')
print("\n")
#下载图片
def download_pic(i,url,pic_name,Type,referer):
head = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
'Referer': referer
}
response = requests.get(url,headers = head)
#下载到对应文件夹
try:
file = open(f'./图片/{Type_list_cn[Type]}/{pic_name}/{i}.jpg','xb')
file.write(response.content)
file.close
except:
print(" 图片已存在!")
#无限循环主程序
while(True):main()