python爬取元气桌面壁纸实例

将url替换成自己想爬取的分类起始页链接,根据需要修改爬取的页数,默认10页,1页有18张壁纸。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import requests
from lxml import etree
import urllib.parse

url = 'https://bizhi.cheetahfun.com/dn/c3j/p53'

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]/img[1]/@src')[0]


split_data = urllib.parse.urlsplit(img_links)


new_links = urllib.parse.urlunsplit((split_data[0], split_data[1], split_data[2], None, None))


file_path = os.path.join(directory, new_links.split('/')[-1])
try:
with open(file_path, 'wb') as f:
f.write(requests.get(new_links).content)
print('文件已保存到本地:{}'.format(file_path))
except Exception as e:
print('保存文件时出错:{}'.format(e))

url = new_url