import requests
from lxml import etree
import os

url = 'http://simpledesktops.com/browse/'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
r = requests.get(url=url,headers=headers)
html = etree.HTML(r.text)   # 接收request数据
data_list = html.xpath('//img/@src')
os.mkdir('./img')  # 创建文件夹
for data in data_list:
    data_url = str(data).replace('.png.295x184_q100.png','.png')
    img_list = requests.get(url=data_url,headers=headers).content
    file_name = str(data_url).split('/')[-1].split('.')[0]
    file_path = './img/'+file_name+'.png'
    with open(file_path,'wb') as f:
        f.write(img_list)

运行结果:

image.png

最后修改:2021 年 12 月 21 日
如果觉得我的文章对你有用,请随意赞赏