kw input( 输入搜索的关键字 )
url https://www.duitang.com/search/?kw {} type feed .format(kw)
headers {
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 ,
Accept : text/html,application/xhtml xml,application/xml;q 0.9,image/avif,image/webp,image/apng,*/*;q 0.8,application/signed-exchange;v q 0.9
# 确定文件夹路径
base_path os.path.dirname(__file__)
path os.path.join(base_path, {} .format(kw))
if not os.path.exists(path):
os.makedirs(path)
# print(path)
html requests.get(url, headers headers)
xhtml etree.HTML(html.text)
src xhtml.xpath( //a[ class a ]/img/ src )
title xhtml.xpath( //a[ class a ]/img/ data-rootid )
# print(title)
for i in range(len(src)):
img_src requests.get(url src[i], headers headers).content
pic_path path ./{} .format(kw) title[i] .jpg
with open(pic_path, wb ) as f:
f.write(img_src)
print( 正在保存第{}张 剩{}张 .format(i 1, len(src) - i - 1))
1.输入搜索关键字
2.生成关键字文件夹
3.批量保存图片到对应的文件夹中
效果如下