网页分析
网址选取
- 由于必应壁纸官方网址很难爬取
所以我们选择其他保存了必应图片的网站
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设置
设置完毕,电脑壁纸就可以自动更换
本文介绍了如何使用Python的Selenium库来爬取一个网站上的必应每日壁纸。通过分析网页结构,抓取图片下载链接,并模拟页面滚动加载新图片,最终实现批量下载壁纸。代码中包含详细的步骤和注释,适用于初学者学习网页爬虫和自动化下载。
1580

被折叠的 条评论
为什么被折叠?



