爬虫项目实战十三:爬取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

相关文章

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)

《JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)》:本文主要介绍如何在IntelliJIDEA2020.1中创建和部署一个JavaWeb项目,包括创建项目、配置Tomcat服务... 目录简介:一、创建项目二、tomcat部署1、将tomcat解压在一个自己找得到路径2、在idea中添加

解决idea启动项目报错java: OutOfMemoryError: insufficient memory

《解决idea启动项目报错java:OutOfMemoryError:insufficientmemory》:本文主要介绍解决idea启动项目报错java:OutOfMemoryError... 目录原因:解决:总结 原因:在Java中遇到OutOfMemoryError: insufficient me

python项目环境切换的几种实现方式

《python项目环境切换的几种实现方式》本文主要介绍了python项目环境切换的几种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 如何在不同python项目中,安装不同的依赖2. 如何切换到不同项目的工作空间3.创建项目

SpringBoot项目整合Netty启动失败的常见错误总结

《SpringBoot项目整合Netty启动失败的常见错误总结》本文总结了SpringBoot集成Netty时常见的8类问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、端口冲突问题1. Tomcat与Netty端口冲突二、主线程被阻塞问题1. Netty启动阻

python项目打包成docker容器镜像的两种方法实现

《python项目打包成docker容器镜像的两种方法实现》本文介绍两种将Python项目打包为Docker镜像的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录简单版:(一次成功,后续下载对应的软件依赖)第一步:肯定是构建dockerfile,如下:第二步

Python + Streamlit项目部署方案超详细教程(非Docker版)

《Python+Streamlit项目部署方案超详细教程(非Docker版)》Streamlit是一款强大的Python框架,专为机器学习及数据可视化打造,:本文主要介绍Python+St... 目录一、针对 Alibaba Cloud linux/Centos 系统的完整部署方案1. 服务器基础配置(阿里

Java 队列Queue从原理到实战指南

《Java队列Queue从原理到实战指南》本文介绍了Java中队列(Queue)的底层实现、常见方法及其区别,通过LinkedList和ArrayDeque的实现,以及循环队列的概念,展示了如何高效... 目录一、队列的认识队列的底层与集合框架常见的队列方法插入元素方法对比(add和offer)移除元素方法

Spring Boot基于 JWT 优化 Spring Security 无状态登录实战指南

《SpringBoot基于JWT优化SpringSecurity无状态登录实战指南》本文介绍如何使用JWT优化SpringSecurity实现无状态登录,提高接口安全性,并通过实际操作步骤... 目录Spring Boot 实战:基于 JWT 优化 Spring Security 无状态登录一、先搞懂:为什

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.