使用 Python 爬取某网站简历模板(bs4/lxml+协程)
使用 Python 爬取站长素材简历模板
简介
在本教程中,我们将学习如何使用 Python 来爬取站长素材网站上的简历模板。我们将使用requests
和BeautifulSoup
库来发送 HTTP 请求和解析 HTML 页面。本教程将分为两个部分:第一部分是使用BeautifulSoup
的方法,第二部分是使用lxml
的方法,并比较两者的差异。
环境准备
首先,确保你已经安装了 Python。然后,安装以下库:
pip install requests beautifulsoup4 lxml
方法一:使用 BeautifulSoup
1.导入库
import requests
from bs4 import BeautifulSoup
import os
2.创建文件夹用于保存爬取的简历图片
if not os.path.exists("resume_templates_images"):os.makedirs("resume_templates_images")
3.爬取第一页
first_page_url = "https://sc.chinaz.com/jianli/free.html"
response = requests.get(first_page_url)
response.encoding = 'utf-8'if response.status_code == 200:soup = BeautifulSoup(response.text, 'html.parser')templates = soup.find_all('div', class_='box col3 ws_block')for template in templates:link = template.find('a', target='_blank')['href']img = template.find('img')['src']if img.startswith('//'):img = 'https:' + imgtitle = template.find('p').find('a').text.strip()img_response = requests.get(img)if img_response.status_code == 200:img_name = f"{title.replace(' ', '_')}.jpg"img_path = os.path.join("resume_templates_images", img_name)with open(img_path, 'wb') as f:f.write(img_response.content)else:print(f"下载图片 {img} 失败,状态码: {img_response.status_code}")
4.爬取第二页到第五页
在这里插入代base_url = "https://sc.chinaz.com/jianli/free_"
for page_num in range(2, 6):url = f"{base_url}{page_num}.html"response = requests.get(url)response.encoding = 'utf-8'if response.status_code == 200:soup = BeautifulSoup(response.text, 'html.parser')templates = soup.find_all('div', class_='box col3 ws_block')for template in templates:link = template.find('a', target='_blank')['href']img = template.find('img')['src']if img.startswith('//'):img = 'https:' + imgtitle = template.find('p').find('a').text.strip()img_response = requests.get(img)if img_response.status_code == 200:img_name = f"{title.replace(' ', '_')}.jpg"img_path = os.path.join("resume_templates_images", img_name)with open(img_path, 'wb') as f:f.write(img_response.content)else:print(f"下载图片 {img} 失败,状态码: {img_response.status_code}")
码片
方法二:使用 lxml
first_page_url = "https://sc.chinaz.com/jianli/free.html"
response = requests.get(first_page_url)
response.encoding = 'utf-8'if response.status_code == 200:tree = etree.HTML(response.text)templates = tree.xpath('//div[@class="box col3 ws_block"]')for template in templates:link = template.xpath('.//a[@target="_blank"]/@href')[0]img = template.xpath('.//img/@src')[0]if img.startswith('//'):img = 'https:' + imgtitle = template.xpath('.//p/a[@class="title_wl"]/text()')[0].strip()img_response = requests.get(img)if img_response.status_code == 200:img_name = f"{title.replace(' ', '_')}.jpg"img_path = os.path.join("resume_templates_images", img_name)with open(img_path, 'wb') as f:f.write(img_response.content)else:print(f"下载图片 {img} 失败,状态码: {img_response.status_code}")
同方法一,但使用lxml
的xpath
方法。
方法比较
• 解析速度:lxml
通常比BeautifulSoup
快,特别是在处理大型 HTML 文档时。
• 易用性:BeautifulSoup
提供了更直观的方法来查找元素,如find
和find_all
,而lxml
使用xpath
,这可能需要更多的学习。
• 灵活性:xpath
在定位复杂的 HTML 结构时更加灵活,但也需要更复杂的查询。
通过运行我们发现这段代码的执行时间较长,那么我们有没有方法来缩短运行时间呢
import asyncio
import aiohttp
from bs4 import BeautifulSoup
import os
import time # 导入time模块来记录时间# 创建一个文件夹resume_templates_images用于保存图片
if not os.path.exists("resume_templates_images"):os.makedirs("resume_templates_images")# 用于存储所有页面的模板数据
all_template_data = []async def fetch(session, url):async with session.get(url) as response:return await response.text()async def parse_page(session, url):soup = BeautifulSoup(await fetch(session, url), 'html.parser')templates = soup.find_all('div', class_='box col3 ws_block')for template in templates:link = template.find('a', target='_blank')['href']img = template.find('img')['src']if img.startswith('//'):img = 'https:' + imgtitle = template.find('p').find('a').text.strip()async with session.get(img) as img_response:if img_response.status == 200:img_name = f"{title.replace(' ', '_')}.jpg"img_path = os.path.join("resume_templates_images", img_name)with open(img_path, 'wb') as f:f.write(await img_response.read())all_template_data.append({'title': title,'img_url': img,'link': link})async def main():start_time = time.time() # 记录开始时间async with aiohttp.ClientSession() as session:# 处理第一页await parse_page(session, "https://sc.chinaz.com/jianli/free.html")# 处理第二页到第五页for page_num in range(2, 6):url = f"https://sc.chinaz.com/jianli/free_{page_num}.html"await parse_page(session, url)# 输出所有页面的模板数据for idx, data in enumerate(all_template_data, 1):print(f"模板 {idx}:")print(f"名称: {data['title']}")print(f"图片链接: {data['img_url']}")print(f"模板链接: {data['link']}")print("=" * 50)end_time = time.time() # 记录结束时间run_time = end_time - start_time # 计算运行时间print(f"程序运行时间:{run_time:.2f}秒")if __name__ == "__main__":asyncio.run(main())
这段代码是一个使用asyncio
和aiohttp
库来异步爬取站长素材网站上的简历模板的 Python 脚本。以下是代码的详细解释和如何加快爬取速度的说明:
• parse_page 函数:一个异步函数,用于解析页面内容,提取模板链接和图片链接,并下载图片。
• 异步 I/O:使用asyncio
和aiohttp
可以实现异步 I/O 操作,这意味着在等待网络响应时,程序可以执行其他任务,而不是被阻塞。这样可以显著提高爬取效率,特别是在需要处理多个页面时。
这段代码是顺序并发执行执行每个页面的爬取,有没有更快的方式——并发执行
• 并发请求:使用asyncio.gather
来同时启动多个parse_page
任务。
修改代码以实现并发请求
以下是如何修改main
函数来实现并发请求:
async def main():start_time = time.time() # 记录开始时间async with aiohttp.ClientSession() as session:# 处理第一页tasks = [parse_page(session, "https://sc.chinaz.com/jianli/free.html")]# 处理第二页到第五页,并发执行for page_num in range(2, 6):url = f"https://sc.chinaz.com/jianli/free_{page_num}.html"tasks.append(parse_page(session, url))# 等待所有页面处理完成await asyncio.gather(*tasks)# 输出所有页面的模板数据for idx, data in enumerate(all_template_data, 1):print(f"模板 {idx}:")print(f"名称: {data['title']}")print(f"图片链接: {data['img_url']}")print(f"模板链接: {data['link']}")print("=" * 50)end_time = time.time() # 记录结束时间run_time = end_time - start_time # 计算运行时间print(f"程序运行时间:{run_time:.2f}秒")if __name__ == "__main__":asyncio.run(main())
在这个修改后的版本中,所有的页面爬取任务都被添加到一个列表中,然后使用asyncio.gather
来并发执行这些任务。这样可以同时发送多个请求,而不是等待一个请求完成后再发送下一个请求,从而加快整体的爬取速度。
import asyncio
import aiohttp
from bs4 import BeautifulSoup
import os
import time
import aiofiles# 创建一个文件夹resume_templates_images用于保存图片
if not os.path.exists("resume_templates_images"):os.makedirs("resume_templates_images")# 用于存储所有页面的模板数据
all_template_data = []
#async with aiohttp.ClientSession() as session
async def fetch(session, url):async with session.get(url) as response:return await response.text()#返回字符串形式的响应数据async def parse_page(session, url):soup = BeautifulSoup(await fetch(session, url), 'html.parser')templates = soup.find_all('div', class_='box col3 ws_block')for template in templates:link = template.find('a', target='_blank')['href']img = template.find('img')['src']if img.startswith('//'):img = 'https:' + imgtitle = template.find('p').find('a').text.strip()async with session.get(img) as img_response:if img_response.status == 200:file_type = ".jpg.rar"# 以rar压缩文件的形式储存img_name = f"{title.replace(' ', '_')+file_type}"# 更改保存的格式仅需修改img_path = os.path.join("resume_templates_images", img_name)async with aiofiles.open(img_path, 'wb') as f:await f.write(await img_response.read())# read()返回二进制数据all_template_data.append({'title': title,'img_url': img,'link': link})async def main():start_time = time.time() # 记录开始时间async with aiohttp.ClientSession() as session:# 创建任务列表tasks = []# 处理第一页task = asyncio.create_task(parse_page(session, "https://sc.chinaz.com/jianli/free.html"))tasks.append(task)# 处理第二页到第五页,并发执行for page_num in range(2, 6):url = f"https://sc.chinaz.com/jianli/free_{page_num}.html"task = asyncio.create_task(parse_page(session, url))tasks.append(task)# 等待所有页面处理完成 挂起任务列表 asyncio.gather 是 Python asyncio 模块中的一个函数,它用于并发地运行多个协程,并且等待它们全部完成。# asyncio.gather 的作用类似于 asyncio.wait,但它不仅等待协程完成,还会返回一个包含所有结果的列表。await asyncio.gather(*tasks)# 输出所有页面的模板数据for idx, data in enumerate(all_template_data, 1):print(f"模板 {idx}:")print(f"名称: {data['title']}")print(f"图片链接: {data['img_url']}")print(f"模板链接: {data['link']}")print("=" * 50)end_time = time.time() # 记录结束时间run_time = end_time - start_time # 计算运行时间print(f"程序运行时间:{run_time:.2f}秒")if __name__ == "__main__":asyncio.run(main())
相关文章:

使用 Python 爬取某网站简历模板(bs4/lxml+协程)
使用 Python 爬取站长素材简历模板 简介 在本教程中,我们将学习如何使用 Python 来爬取站长素材网站上的简历模板。我们将使用requests和BeautifulSoup库来发送 HTTP 请求和解析 HTML 页面。本教程将分为两个部分:第一部分是使用BeautifulSoup的方法&am…...

深度学习模型中音频流式处理
音频流式处理的介绍 在现代深度学习应用中,音频处理是一个重要的领域,尤其是在语音识别、音乐生成和音频分类等任务中。流式处理(Streaming Processing)是一种有效的处理方式,它允许模型逐帧处理音频数据,…...
C语言(字符数组和字符指针)
字符串实现 在C语言中,表示一个字符串有以下两种形式: 用字符数组存放一个字符串。用字符指针指向一个字符串。 案例 #include <stdio.h>/*** 方式1:使用字符数组实现字符串*/ void str_test1(){// 定义一个伪字符串char str[] &q…...

SkyWalking Helm Chart 4.7.0 安装、配置
https://skywalking.apache.org/events/release-apache-skywalking-kubernetes-helm-chart-4.7.0/https://github.com/apache/skywalking-helm/tree/v4.7.0https://skywalking.apache.org/zh/2020-04-19-skywalking-quick-start/简介 skywalking 是分布式系统的 APM(Applicat…...

微搭低代码AI组件单词消消乐从0到1实践
目录 1 为什么要开发单词消消乐2 需要具备什么功能3 采用什么技术方案实现4 逻辑设计4.1 数据结构设计4.2 游戏的核心逻辑4.3 数据设计 5 代码详解5.1 导入依赖5.2 定义函数组件5.3 数据初始化5.4 状态定义5.5 打乱解释的逻辑5.6 定义选择单词的函数5.7 定义选择解释的函数5.8 …...
23种设计模式之中介者模式
目录 1. 简介2. 代码2.1 Mediator (中介者接口)2.2 ChatRoom (具体中介者类)2.3 User (同事接口)2.4 ChatUser (具体同事类)2.5 Test (测试)2.6 运行结果 3. …...

【Golang】Go语言编程思想(六):Channel,第六节,并发编程模式
并发模式 下例重新对 channel 的用法进行回顾: package mainimport ("fmt""math/rand""time" )func msgGen(name string) chan string {c : make(chan string)go func(name string) { // 在这个 goroutine 当中向外发送数据i : 0fo…...

unity打包web,如何减小文件体积,特别是 Build.wasm.gz
unity打包WebGL,使用的是wasw,最终生成的Build.wasm.gz体积很大,有6.5M,有几个方法可以稍微减小这个文件的大小 1. 裁剪引擎代码: 此步可将大小从6.5减小到 6.2(此项默认开启,只是改了裁剪等级…...

go引入skywalking
前置条件:安装好jdk11,linux服务器(centos7.9),go版本(我的是1.18,1.21都可以) 1.下载skywalking Downloads | Apache SkyWalking 2.下载agent源码 Downloads | Apache SkyWalkin…...
大华DSS数字监控系统 attachment_downloadAtt.action 任意文件下载漏洞复现
0x01 产品描述: 大华 DSS 数字监控系统是大华开发的一款安防视频监控系统,拥有实时监视、云台操作、录像回放、报警处理、设备管理等功能。0x02 漏洞描述: 大华DSS数字监控系统 attachment_downloadAtt.action接口存在任意文件读取漏洞,未经身份验证攻击者可通过该漏洞读取…...

qt 封装 调用 dll
这个目录下 ,第一个收藏的这个 ,可以用, 但是有几个地方要注意 第一.需要将dll的头文件添加到qt的文件夹里面 第二,需要在pro文件里面添加动态库路径 第三,如果调用dll失败,那么大概需要将dll文件放在e…...

Python使用Selenium库获取 网页节点元素、名称、内容的方法
我们要用到一些网页源码信息,例如获取一些节点的class内容, 除了使用Beautifulsoup来解析,还可以直接用Selenium库打印节点(元素)名称,用来获取元素的文本内容或者标签名。 例如获取下面的class的内容&am…...

系统安全——访问控制访问控制
访问控制 概念 什么是访问控制 access control 为用户对系统资源提供最大限度共享的基础上,对用户的访问权进行管理,防止对信息的非授权篡改和滥用 访问控制作用 保证用户在系统安全策略下正常工作 拒绝非法用户的非授权访问请求 拒绝合法用户越权…...
SQL Server 数据库还原到某个时点(完整恢复模式)
将 SQL Server 数据库还原到某个时点(完整恢复模式) 适用范围: SQL Server 本主题介绍如何使用 SQL Server Management Studio 或 Transact-SQL 将数据库还原到 SQL Server 中的某个时间点。 本主题仅与使用完整恢复模式或大容量日志恢复模…...

埃隆马斯克X-AI发布Grok-2大模型,快来体验~
引言 近年来,人工智能技术的快速发展推动了大语言模型的广泛应用。无论是日常生活中的智能助手,还是行业中的自动化解决方案,大语言模型都扮演着越来越重要的角色。2024年,X-AI推出了新一代的大模型——Grok-2,这款模…...
Python工厂设计模式:简化对象创建
Python工厂设计模式:简化对象创建 引言什么是工厂模式?简单工厂模式示例定义基类和子类创建工厂类使用工厂创建对象 优点使用场景总结 引言 在编程中,我们经常需要创建不同的对象,但有时创建对象的逻辑可能会变得复杂。工厂设计模…...

【隐私计算篇】隐私集合求交(PSI)原理深入浅出
隐私集合求交技术是多方安全计算领域的一个子问题,通常也被称为安全求交、隐私保护集合交集或者隐私交集技术等,其目的是允许持有各自数据集的双方或者多方,执行两方或者多方集合的交集计算,当PSI执行完成,一方或者两方…...
工作中常用的8种设计模式
前言 设计模式在我们日常的软件开发中无处不在,它们帮助我们编写更易扩展、更具可读性的代码。 今天结合我实际工作场景和源码实例,跟大家一起聊聊工作中最常用的8种设计模式,希望对你会有所帮助。 1. 单例模式 单例模式确保一个类只有一…...

Qwen 论文阅读记录
本文仅作自己初步熟悉大模型,梳理之用,慢慢会更改/增加/删除,部分细节尚未解释,希望不断学习之后,能够完善补充。若有同道之人,欢迎指正探讨。 关于后面的code-qwen and math-qwen,我个人认为依…...
自动驾驶:百年演进
亲爱的小伙伴们😘,在求知的漫漫旅途中,若你对深度学习的奥秘、JAVA 、PYTHON与SAP 的奇妙世界,亦或是读研论文的撰写攻略有所探寻🧐,那不妨给我一个小小的关注吧🥰。我会精心筹备,在…...
Python爬虫实战:研究feedparser库相关技术
1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
uni-app 中 Web-view 与 Vue 页面的通讯机制详解 一、Web-view 简介 Web-view 是 uni-app 提供的一个重要组件,用于在原生应用中加载 HTML 页面: 支持加载本地 HTML 文件支持加载远程 HTML 页面实现 Web 与原生的双向通讯可用于嵌入第三方网页或 H5 应…...
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数 在软件开发中,单例模式(Singleton Pattern)是一种常见的设计模式,确保一个类仅有一个实例,并提供一个全局访问点。在多线程环境下,实现单例模式时需要注意线程安全问题,以防止多个线程同时创建实例,导致…...
【Go语言基础【12】】指针:声明、取地址、解引用
文章目录 零、概述:指针 vs. 引用(类比其他语言)一、指针基础概念二、指针声明与初始化三、指针操作符1. &:取地址(拿到内存地址)2. *:解引用(拿到值) 四、空指针&am…...
Caliper 配置文件解析:fisco-bcos.json
config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...
Kafka主题运维全指南:从基础配置到故障处理
#作者:张桐瑞 文章目录 主题日常管理1. 修改主题分区。2. 修改主题级别参数。3. 变更副本数。4. 修改主题限速。5.主题分区迁移。6. 常见主题错误处理常见错误1:主题删除失败。常见错误2:__consumer_offsets占用太多的磁盘。 主题日常管理 …...

PydanticAI快速入门示例
参考链接:https://ai.pydantic.dev/#why-use-pydanticai 示例代码 from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel from pydantic_ai.providers.openai import OpenAIProvider# 配置使用阿里云通义千问模型 model OpenAIMode…...

SQL注入篇-sqlmap的配置和使用
在之前的皮卡丘靶场第五期SQL注入的内容中我们谈到了sqlmap,但是由于很多朋友看不了解命令行格式,所以是纯手动获取数据库信息的 接下来我们就用sqlmap来进行皮卡丘靶场的sql注入学习,链接:https://wwhc.lanzoue.com/ifJY32ybh6vc…...

Linux入门课的思维导图
耗时两周,终于把慕课网上的Linux的基础入门课实操、总结完了! 第一次以Blog的形式做学习记录,过程很有意思,但也很耗时。 课程时长5h,涉及到很多专有名词,要去逐个查找,以前接触过的概念因为时…...

新版NANO下载烧录过程
一、序言 搭建 Jetson 系列产品烧录系统的环境需要在电脑主机上安装 Ubuntu 系统。此处使用 18.04 LTS。 二、环境搭建 1、安装库 $ sudo apt-get install qemu-user-static$ sudo apt-get install python 搭建环境的过程需要这个应用库来将某些 NVIDIA 软件组件安装到 Je…...