想爬取必应做壁纸?这样做就够啦!

本文介绍了如何使用Python的Selenium库来爬取一个网站上的必应每日壁纸。通过分析网页结构,抓取图片下载链接,并模拟页面滚动加载新图片,最终实现批量下载壁纸。代码中包含详细的步骤和注释,适用于初学者学习网页爬虫和自动化下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网页分析

网址选取

  • 由于必应壁纸官方网址很难爬取
    所以我们选择其他保存了必应图片的网站
    http://www.zhishile.com/

图片下载地址分析

点击图片进入的网址为

http://www.zhishile.com/Wallpaper/20210708
http://www.zhishile.com/Wallpaper/20210707
http://www.zhishile.com/Wallpaper/20210706

在首页处进行抓包

<div class="card"><a class="ui image" href="/Wallpaper/20210707"><img src="/BingWallpapers/20210707/wallpaper_20210707.jpg"></a><div class="content"><div class="header">乌鲁米耶湖</div><div class="meta">伊朗</div></div><div class="extra content"><span class="right floated margin_left"><i class="unhide icon"></i> 999</span><span class="right floated"><i class="cloud download icon"></i> 888</span><span class="ui left floated teal label">2021-07-07</span></div></div>

可以看出要抓取href="/Wallpaper/20210707"的信息
与http://www.zhishile.com拼接即是图片网址

点击进入http://www.zhishile.com/Wallpaper/20210707
对下载地址进行抓包分析

<div class="options">
            <a class="left floated circular ui icon button" onclick="window.history.go(-1);" data-tooltip="返回首页" data-position="right center">
                <i class="large laptop icon"></i>
            </a>
            <a id="btn_down" class="right floated circular ui icon button" href="/Down/20210707" data-tooltip="下载壁纸" data-position="left center">
                <i class="large arrow down icon"></i>
            </a>
        </div>

我们要抓取的内容为href="/Down/20210707"
经测试,http://www.zhishile.com/Down/20210707确实为下载地址

所以最终只要http://www.zhishile.com/Down/+抓取到的日期

抓取日期分析

这个网页的图片是动态加载的,在下拉时候刷新
所以我们需要模拟下拉操作,下拉到一定程度后再获取网页源代码进行分析

代码

from selenium import webdriver
from time import sleep
from lxml import etree
import os
import requests
if __name__ == "__main__":
    #创建一个文件夹
    if not os.path.exists('./picLibs'):
        os.mkdir('./picLibs')  
        
    bro = webdriver.Chrome(executable_path='E:/chromedriver_win32/chromedriver.exe')    
    bro.get('http://www.zhishile.com/')
    
    #向下拉去30次页面,这个值根据你想下载多少自己设定
    for i in range(1,30):
        sleep(1)#留个时间  刷新下图片,不要太快
        bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')   #每次下滑一个窗口距离
    sleep(1)
    
    #获取标签数据
    page_text = bro.page_source
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//a[@class="ui image"]/@href')
    
    
    for li in li_list:
        image_name=li.split('/')[-1]#分离出日期作为图片名字
        image_url='http://www.zhishile.com/Down/'+image_name
        img_data = requests.get(url=image_url).content
        #下载图片
        with open('./picLibs/'+image_name+'.jpg','wb') as fp:
            fp.write(img_data)
            print(image_name,'下载成功')
    bro.quit()


效果
成功爬取
在这里插入图片描述

windows设置

在这里插入图片描述设置完毕,电脑壁纸就可以自动更换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王蒟蒻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值