python爬取元气桌面壁纸实例

本文最后更新于:2023年12月7日 上午

python爬取元气桌面壁纸实例

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

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

微信二维码

微信支付

支付宝二维码

支付宝支付

“文章不错,请博主喝咖啡☕️”