爬虫项目实战十三:爬取zol桌面壁纸

2023-10-25 15:40

本文主要是介绍爬虫项目实战十三:爬取zol桌面壁纸,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

爬取zol桌面壁纸

      • 目标
      • 项目准备
      • 项目分析
      • 页码分析
      • 反爬分析
      • 代码实现
      • 效果显示

目标

爬取zol桌面壁纸,批量下载图片。

项目准备

软件:Pycharm
第三方库:requests,fake_useragent,re,lxml
网站地址:http://desk.zol.com.cn/1920x1080/

项目分析

打开网站看一下。
在这里插入图片描述
在这里插入图片描述
每一个都是一个图集。
点开
在这里插入图片描述
在这里插入图片描述
查看源代码
在这里插入图片描述
可以看出每一个都可以在源代码中找到。判定为静态网页。

页码分析

第一页url链接:http://desk.zol.com.cn/1920x1080/1.html
第二页url链接:http://desk.zol.com.cn/1920x1080/2.html
第三页url链接:http://desk.zol.com.cn/1920x1080/3.html

可以发现每一页随着后面的数字而变化。
在这里插入图片描述

反爬分析

同一个ip地址去多次访问会面临被封掉的风险,这里采用fake_useragent,产生随机的User-Agent请求头进行访问。

代码实现

1.导入相对应的第三方库,定义一个class类继承object,定义init方法继承self,主函数main继承self。

import requests
from fake_useragent import UserAgent
import re
from lxml import etree
class bizhi(object):def __init__(self):self.url = 'http://desk.zol.com.cn/1920x1080/hot_{}.html'ua = UserAgent(verify_ssl=False)for i in range(1, 100):self.headers = {'User-Agent': ua.random}def main(self):pass
if __name__ == '__main__':spider = bizhi()spider.main()

2.发送请求,获取网页。

    def get_html(self,url):response = requests.get(url, headers=self.headers)html = response.content.decode('gb2312')return html

注:html = response.content.decode(‘gb2312’)
在这里插入图片描述
3.获取每个图集的url

    def get_link(self,html):target=etree.HTML(html)links=target.xpath('//li[@class="photo-list-padding"]/a/@href')for link in links:print('http://desk.zol.com.cn'+link)

4.获取每个图集中每个图片的链接
在这里插入图片描述

在这里插入图片描述

host='http://desk.zol.com.cn'+link
res = requests.get(host, headers=self.headers)
htm=res.text
images=re.compile('<img src="(.*?)" width="144" height="90" >').findall(htm)for image in images:print(image)

但是这样获取的图片太小了。
在这里插入图片描述

https://desk-fd.zol-img.com.cn/t_s144x90c5/g6/M00/0C/08/ChMkKV9PGV6IdfukAClWngY1Z3QAABx1wO9BUkAKVa2874.jpg
在这里插入图片描述
只有144x90的大小。
尝试一下修改:https://desk-fd.zol-img.com.cn/t_s1920x1080c5/g6/M00/0C/08/ChMkKV9PGV6IdfukAClWngY1Z3QAABx1wO9BUkAKVa2874.jpg

打开看一下
在这里插入图片描述
OK没问题。
5.批量下载到本地。

    def get_link(self,html):global filenametarget=etree.HTML(html)links=target.xpath('//li[@class="photo-list-padding"]/a/@href')for link in links:#print('http://desk.zol.com.cn'+link)host='http://desk.zol.com.cn'+linkres = requests.get(host, headers=self.headers)htm=res.text#t=etree.HTML(htm)#images=t.xpath('//div[@class="photo-list-box"]/ul/li/a/img/@src')images=re.compile('<img src="(.*?)144x90(.*?)" width="144" height="90" >').findall(htm)for image in images:print(image[0]+'1920x1080'+image[1])result_url=image[0]+'1920x1080'+image[1]r=requests.get(result_url,headers=self.headers)with open('F:/pycharm文件/photo/' + str(filename) + '.jpg', 'wb') as f:f.write(r.content)filename+=1

6.主函数及函数调用。

    def main(self):end_page = int(input("要爬多少页:"))for page in range(1, end_page + 1):url = self.url.format(page)print("第%s页。。。。" % page)html=self.get_html(url)self.get_link(html)

效果显示

在这里插入图片描述
看一下本地目录。
在这里插入图片描述
完整代码如下:

import requests
from fake_useragent import UserAgent
import re
from lxml import etree
filename=0
class bizhi(object):def __init__(self):self.url = 'http://desk.zol.com.cn/1920x1080/hot_{}.html'ua = UserAgent(verify_ssl=False)for i in range(1, 100):self.headers = {'User-Agent': ua.random}def get_html(self,url):response = requests.get(url, headers=self.headers)html = response.content.decode('gb2312')return htmldef get_link(self,html):global filenametarget=etree.HTML(html)links=target.xpath('//li[@class="photo-list-padding"]/a/@href')for link in links:#print('http://desk.zol.com.cn'+link)host='http://desk.zol.com.cn'+linkres = requests.get(host, headers=self.headers)htm=res.text#t=etree.HTML(htm)#images=t.xpath('//div[@class="photo-list-box"]/ul/li/a/img/@src')images=re.compile('<img src="(.*?)144x90(.*?)" width="144" height="90" >').findall(htm)for image in images:print(image[0]+'1920x1080'+image[1])result_url=image[0]+'1920x1080'+image[1]r=requests.get(result_url,headers=self.headers)with open('F:/pycharm文件/photo/' + str(filename) + '.jpg', 'wb') as f:f.write(r.content)filename+=1def main(self):end_page = int(input("要爬多少页:"))for page in range(1, end_page + 1):url = self.url.format(page)print("第%s页。。。。" % page)html=self.get_html(url)self.get_link(html)
if __name__ == '__main__':spider = bizhi()spider.main()

声明:仅作为自己学习参考使用。

这篇关于爬虫项目实战十三:爬取zol桌面壁纸的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/283539

相关文章

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll