from lxml import etree
import requests
# 在此处修改帖子的链接
url = 'https://wallhaven.cc/search?categories=001&purity=010&topRange=1y&sorting=toplist&order=desc&ai_art_filter=1&page=3'
respond = requests.get(url).text
selector = etree.HTML(respond)
image_urls = selector.xpath('//a[@class="preview"]/@href')
index = 24
for image_url in image_urls:
respond1 = requests.get(image_url).text
selector1 = etree.HTML(respond1)
image_url2 = str(selector1.xpath('//img[@id="wallpaper"]/@src'))
image_url2 = image_url2.strip('[\']')
print(image_url2)
image_content = requests.get(image_url2).content
with open('{}.jpg'.format(index), 'wb') as f:
f.write(image_content)
index += 1
爬取某不知名网站壁纸
最新推荐文章于 2024-11-03 09:30:00 发布
该代码使用Python的requests和lxml库从Wallhaven网站抓取高排名的AI艺术壁纸链接,然后下载图片到本地。它首先获取页面HTML,使用XPath提取图片URL,再单独请求每个图片URL并保存为.jpg文件。
摘要由CSDN通过智能技术生成