前言
嗨喽~大家好呀,这里是魔王呐 !
![](https://img.haomeiwen.com/i13117506/2e85932db1804300.png)
壁纸,有多种的类别和各种不同的风格,如:
风景、美女、唯美、动漫、花卉、节日等适合您的高清桌面壁纸
![](https://img.haomeiwen.com/i13117506/5981c5e916df4d10.jpg)
今天我们就来采集一下叭~
环境使用:
-
Python 3.8 解释器
-
Pycharm 编辑器
需安装python第三方模块 : requests
-
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
-
在pycharm中点击Terminal(终端) 输入安装命令
基本思路流程:
1. 发送请求
模拟浏览器 对于url地址发送请求, 获取服务器返回响应数据
伪装 headers 请求头
2. 获取数据
3. 解析数据
提取我们想要的内容
4. 保存数据
代码
import requests # 用来发送请求模块<工具>
import re # 提取数据工具
![](https://img.haomeiwen.com/i13117506/a1948e9e02e896b6.png)
response = requests.get(url=url, headers=headers)
response.encoding = 'gbk' # 获取网页内容,返回出现乱码
print(response.text) # 获取网页源代码
# 获取壁纸名字以及壁纸详情页url地址 从什么地方找什么样数据内容, 从response.text 里面找<li><a href="(.*?)"title="(.*?)" target="_blank">
# (.*?) 就是我们想要数据
html_info = re.findall('<li><a href="(.*?)" title="(.*?)" target="_blank">', response.text)
print(html_info)
![](https://img.haomeiwen.com/i13117506/b989ee270909cda2.png)
response_1 = requests.get(url=link_url, headers=headers)
response_1.encoding = 'gbk'
# print(response_1.text)
img_url = re.findall('<img src="(.*?)" alt=".*?"', response_1.text)[0]
img_content = requests.get(url=img_url).content
with open('img\\' + title + '.jpg', mode='wb') as f:
f.write(img_content)
print(img_url, title)
效果
![](https://img.haomeiwen.com/i13117506/5e0ec93a1349c02f.png)
![](https://img.haomeiwen.com/i13117506/9435a32a1870a45c.png)
![](https://img.haomeiwen.com/i13117506/2349227a9f1b10ed.jpg)
![](https://img.haomeiwen.com/i13117506/0a33d1e4c72e84a9.jpg)
![](https://img.haomeiwen.com/i13117506/03bb583d70c6e171.jpg)
![](https://img.haomeiwen.com/i13117506/4a225baf41a425ec.jpg)
![](https://img.haomeiwen.com/i13117506/f09a15ffc8b96abc.jpg)
![](https://img.haomeiwen.com/i13117506/72649e2d5dac67a8.jpg)
![](https://img.haomeiwen.com/i13117506/9ffcd52c3c276231.jpg)
![](https://img.haomeiwen.com/i13117506/a98ce7812a413ea2.jpg)
![](https://img.haomeiwen.com/i13117506/fcf955d61b312ac2.jpg)
尾语
要成功,先发疯,下定决心往前冲!
学习是需要长期坚持的,一步一个脚印地走向未来!
未来的你一定会感谢今天学习的你。
—— 心灵鸡汤
本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦 😝
![](https://img.haomeiwen.com/i13117506/f7c5a42066ece093.gif)
网友评论