批量爬取美女图片
import re
import os
import requests
if __name__=="__main__":
folder = os.path.exists("./girls")
if not folder:
path = os.makedirs("./girls")
for i in range(1,11):
url='https://smtmm.win/?'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36 Edg/89.0.774.77'
}
data={
'page':i
}
html_data=requests.get(url=url,headers=headers,params=data).text
pattern='<a class="thumbnail" href="(.*?)">'
o=re.findall(pattern,html_data,re.S)
for l in o:
url_1='https://smtmm.win'+l
html_data1=requests.get(url=url_1,headers=headers).text
with open("./w.html", 'w', encoding='utf-8')as f:
f.write(html_data1)
pattern='<p>.*?<.*?data-original="(.*?)" alt.*?/>'
img_data=re.findall(pattern,html_data1,re.S)
x = 0
pattern_1 = f'<h2><a href="{l}">(.*?)</a></h2>'
m=re.findall(pattern_1,html_data,re.S)
folders=os.path.exists(f"./girls/{m[0]}")
if not folders:
path=os.makedirs(f"./girls/{m[0]}")
for n in img_data:
x+=1
url="https://smtmm.win"+n
imgs_data=requests.get(url=url).content
filename=f"./girls/{m[0]}/{m[0]}{x}.jpg"
with open(filename,'wb') as f:
f.write(imgs_data)
print(filename.split('/')[-1],"下载成功!")