python爬取元气桌面动态壁纸实例
本文最后更新于:2024年12月14日 下午
python爬取元气桌面动态壁纸实例
将url替换成自己想爬取的分类起始页链接,根据需要修改爬取的页数,默认10页,1页有18个mp4视频。
import os
import requests
from lxml import etree
url = 'https://bizhi.cheetahfun.com/dn/c3d/p2'
headesp = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35'}
for i in range(10):
num_str = url.split('/p')[-1]
num = int(num_str) + 1
new_url = url[:-len(num_str)] + str(num)
response = requests.get(new_url, headers=headesp)
html = response.content
selector = etree.HTML(html)
links = selector.xpath('/html/body/div/div/div/div[1]/main/div/div[1]/section/ul/li/div/a/@href')
directory = '元气动态'
if not os.path.exists(directory):
os.makedirs(directory)
for link in links:
response = requests.get(link)
html = response.content
selector = etree.HTML(html)
img_links = selector.xpath('/html/body/div/div/div/div[1]/main/div/div[1]/div/div[2]/div[1]/video/@src')[0]
file_path = os.path.join(directory, img_links.split('/')[-1])
try:
with open(file_path, 'wb') as f:
f.write(requests.get(img_links).content)
print('文件已保存到本地:{}'.format(file_path))
except Exception as e:
print('保存文件时出错:{}'.format(e))
url = new_url
微信支付
支付宝支付
“请博主喝咖啡☕️”