[Python] 纯文本查看 复制代码
import requests
from bs4 import BeautifulSoup
import os
import chardet
from chardet import detect
import re
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
'Accept-Encoding': 'gzip',
"Referer": "https://www.meitulu.com/"
} #头部文件
path="jpg" #jpg为保存目录,可以随意更改
os.chdir(path)
url = 'https://www.meitulu.com/t/qingdouke/' #青豆客
#'https://www.meitulu.com/t/youwuguan/' 尤物馆
#'https://www.meitulu.com/t/girlt/' 果团网 , 'https://www.meitulu.com/t/mistar/', 魅妍社
#'https://www.meitulu.com/t/1088/', 星颜社 'https://www.meitulu.com/t/ishow/', iSHOW爱秀
#'https://www.meitulu.com/t/huayan/',花の颜 'https://www.meitulu.com/t/xingleyuan/',星乐园
#'https://www.meitulu.com/t/tukmo/', 'https://www.meitulu.com/t/aiss/',
#'https://www.meitulu.com/t/miitao/', 'https://www.meitulu.com/t/uxing/',
#'https://www.meitulu.com/t/taste/', 'https://www.meitulu.com/t/micat/',
#'https://www.meitulu.com/t/candy/', 'https://www.meitulu.com/t/yunvlang/',
#'https://www.meitulu.com/t/youmihui/', 'https://www.meitulu.com/t/1113/',
#'https://www.meitulu.com/t/1209/', 'https://www.meitulu.com/t/imiss/',
#'https://www.meitulu.com/t/yingsihui-wings/', https://www.meitulu.com/t/dianannan/ 嗲囡囡
#url可更改为上面随意一个
#美图录的随意一个目录url复制即可运行
response1 = requests.get(url, headers=headers, allow_redirects=False)
e = chardet.detect(response1.content)['encoding']
response1.encoding = e
html_soup1 = BeautifulSoup(response1.text, 'lxml')
all_a = html_soup1.find('ul', class_='img').find_all('li') #
count=0
for l in all_a:
count = count + 1
a = l.a
#print(a)
if (a.find('img') == None):
continue
else:
href = a["href"]
print(href)
href2=href.replace('.html', '_')
response2 = requests.get(href, headers=headers)
e = chardet.detect(response2.content)['encoding']
response2.encoding = e
html_soup2 = BeautifulSoup(response2.text, 'lxml')
name = html_soup2.find('title').text.replace('/', '-')
os.mkdir(name)
print(name)
img1 = html_soup2.find("div", {"class": "content"}).find_all("img")
page = html_soup2.find("div", {"id": "pages"}).find_all('a')[-2]
max_page = page.get_text()
for pic in img1:
os.chdir(name)
count = count + 1
pic1 = pic['src']
# print(pic1)
pic2 = requests.get(pic1, headers=headers)
f = open(name+str(count) + '.jpg', 'ab')
f.write(pic2.content)
f.close()
os.chdir("/Users/w2ng/untitled/venv/jpg")
for i in range(2, int(max_page) + 1):
os.chdir(name)
href3 = href2 + str(i) + '.html'
response3 = requests.get(href3, headers=headers)
html_soup3 = BeautifulSoup(response3.text, 'lxml')
img1 = html_soup3.find("div", {"class": "content"}).find_all("img")
# print(href3)
for pic in img1:
count = count + 1
pic1 = pic['src']
# print(pic1)
pic2 = requests.get(pic1, headers=headers)
f = open(name+str(count) + '.jpg', 'ab')
f.write(pic2.content)
f.close()
os.chdir("/Users/w2ng/untitled/venv/jpg") #获取自己的工作目录,将/Users/w2ng/untitled/venv 改为os.getcwd()的结果就ok
print("完成!")