珠海网站建设模板建设网站开发公司

web/2025/9/27 15:29:18/文章来源:
珠海网站建设模板,建设网站开发公司,服装网站的建设背景,用php做视频网站的步骤使用 Python 爬取站长素材简历模板 简介 在本教程中#xff0c;我们将学习如何使用 Python 来爬取站长素材网站上的简历模板。我们将使用requests和BeautifulSoup库来发送 HTTP 请求和解析 HTML 页面。本教程将分为两个部分#xff1a;第一部分是使用BeautifulSoup的方法我们将学习如何使用 Python 来爬取站长素材网站上的简历模板。我们将使用requests和BeautifulSoup库来发送 HTTP 请求和解析 HTML 页面。本教程将分为两个部分第一部分是使用BeautifulSoup的方法第二部分是使用lxml的方法并比较两者的差异。 环境准备 首先确保你已经安装了 Python。然后安装以下库 pip install requests beautifulsoup4 lxml方法一使用 BeautifulSoup 1.导入库 import requests from bs4 import BeautifulSoup import os2.创建文件夹用于保存爬取的简历图片 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-8if 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( , _)}.jpgimg_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}.htmlresponse requests.get(url)response.encoding utf-8if 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( , _)}.jpgimg_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-8if response.status_code 200:tree etree.HTML(response.text)templates tree.xpath(//div[classbox 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[classtitle_wl]/text())[0].strip()img_response requests.get(img)if img_response.status_code 200:img_name f{title.replace( , _)}.jpgimg_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( , _)}.jpgimg_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 fhttps://sc.chinaz.com/jianli/free_{page_num}.htmlawait 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 fhttps://sc.chinaz.com/jianli/free_{page_num}.htmltasks.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 fhttps://sc.chinaz.com/jianli/free_{page_num}.htmltask 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())

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/81083.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

公益网站建设 参考文献网站服务空间

java oca我在业余时间正在阅读Mala Gupta的Oracle认证Java SE程序员助理书,我对所学到的一些新知识感到惊讶。 有时候他们真的没有道理,有时候他们说得通,但真的让人惊讶。 因此,在本系列文章中,我想将它们共享为“ Ja…

seo网站推广的作用淘宝店铺怎么买

一&#xff1a;图的基本概念和术语 1.图之间的关系可以是任意的&#xff0c;任意两个数据元素之间都可能相关。 2.顶点&#xff1a;数据元素。 3.边or弧&#xff1a;从一个顶点到另一个顶点的路径。<V, W>表示弧&#xff0c;&#xff08;V,W&#xff09;表示边&#x…

必应搜索网站甘肃省建设厅查行网站

服务器配置如下&#xff1a; CPU/NPU&#xff1a;鲲鹏 CPU&#xff08;ARM64&#xff09;A300I pro推理卡 系统&#xff1a;Kylin V10 SP1【下载链接】【安装链接】 驱动与固件版本版本&#xff1a; Ascend-hdk-310p-npu-driver_23.0.1_linux-aarch64.run【下载链接】 Ascend-…

怎么做送餐网站wordpress文章密码查看

文章目录 前言一、BRDF中的镜面反射项二、分别解析每一个参数1、D、G函数&#xff1a;speclarTerm2、其他中间步骤3、光照颜色4、F函数&#xff08;菲涅尔函数&#xff09; &#xff1a;FresnelTermIBL在下篇文章中继续解析 三、最终代码.cginc文件:Shader文件&#xff1a; 前言…

网站建设报价比较最好网站建设公司

下载安装 官网 https://www.sublimetext.com 点击跳转 2023.7.21 版本为4143 Windows激活方式 一、激活License方式 入口在菜单栏中"Help” -> “Enter License” 注意格式&#xff0c;可能会过期失效&#xff0c;失效就用方式二 Mifeng User Single User License E…

网站软文写作要求百度云服务器搭建网站步骤

1、知识星球下载&#xff1a; 如需下载完整PPTX可编辑源文件&#xff0c;请前往星球获取&#xff1a;https://t.zsxq.com/19QeHVt8y 2、免费领取步骤&#xff1a; 【1】关注公众号 方案驿站 【2】私信发送 【智慧路灯大数据平台】 【3】获取本方案PDF下载链接&#xff0c;直…

定制网站设计方案外包公司上门催债是合法的吗

遥感卫星综述&#xff08;下载和预处理&#xff09; 目录 遥感卫星综述&#xff08;下载和预处理&#xff09;一、国产卫星GF-1 WFV 二、国外卫星Sentinel-1Sentinel-2 一、国产卫星 GF-1 WFV 下载 分辨率波段16m4(蓝、绿、红、近红) 预处理&#xff1a; ENVI预处理GF-1号W…

网站安全检测今天刚刚发生的新闻最新新闻

文章目录 第1章 搭建开发环境1.1开发套件硬件接口资源介绍1.2资料下载1.3安装Keil MDK1.3.1**软件下载**1.3.2**软件安装**1.3.3 PACK 安装 1.4 安装 STM32CubeMX1.5 安装 STM32CubeProgrammer1.6 安装 ST-Link 驱动1.7 安装 CH340 驱动 第1章 搭建开发环境 1.1开发套件硬件接…

网站可信认证对企业有哪些优势网站建设服务器价格

MySQL初始用 目录 MySQL初始用基本语法约定选择数据库查看数据库和表其它的SHOW 在Navicat中&#xff0c;大部分数据库管理相关的操作都可以通过图形界面完成&#xff0c;这个很简单&#xff0c;大家可以自行探索。虽然Navicat等图形化数据库管理工具为操作和管理数据库提供了非…

php网站开发就业杭州app定制公司

WeihanLi.Npoi 1.18.0 ReleasedIntro前段时间一直在想&#xff0c;把现在的配置做成类似于 AutoMapper 和 FluentValidation 那样&#xff0c;把每个类型的 mapping 配置放在一个类中&#xff0c;这样我们就可以比较好地组织我们的 mapping 关系&#xff0c;也可以配置多个 map…

企业做网站建设网站seo推广软件

目录 字符编码 字符编码说明参考网站 字符编码 ASCII编码 ASCII编码介绍 ASCII编码表 中文编码 1. GB2312标准 区位码 2. GBK编码 3. GB18030 各个标准的对比说明 4. Big5编码 字符编码 字符编码说明参考网站 字符编码及转换测试&#xff1a;导航菜单 - 千千秀字 …

学网站建设多少学费初学网站建设

目 录 前言 1硬件资源 1.1CPU 1.2ROM 1.3RAM 1.4时钟系统 1.5电源 1.6LED

网站如何做吸引人的项目灯箱网站开发

目录 描述 思路 查看ipa包崩溃日志 简单查看手机崩溃信息几种方式 方式1:手机设置查看崩溃日志 方式2: Xocde工具 方式3: 第三方软件克魔助手 环境配置 实时日志 奔溃日志分析 方式四&#xff1a;控制台资源库 线上崩溃日志 线上监听crash的几种方式 方式1: 三方平…

东莞做门户网站注册域名的注意事项

这win下默认的主题令人窒息 打开git bash&#xff0c;使用命令cd ~然后用Atom命令打开文件.minttyrc.atom .minttyrc如果默认没有.minttyrc文件&#xff0c;自己新创建的也行。里面的内容填写如下&#xff1a;FontConsolas FontHeight14ForegroundColour131,148,150 Background…

网站域名设计方案短视频分享网站开发

映射配置 在创建索引时&#xff0c;可以预先定义字段的类型&#xff08;映射类型&#xff09;及相关属性。 数据库建表的时候&#xff0c;我们DDL依据一般都会指定每个字段的存储类型&#xff0c;例如&#xff1a;varchar、int、datetime等&#xff0c;目的很明确&#xff0c;就…

装饰公司 网站模板网站ui设计是什么

vs2010做mvc3 开发,用的是Razor的View,想修改默认浏览器,发现右键没有"浏览方式",把View改成.aspx的,也没有找到这个选项. 解决方法两种 (1)最简单的,建个Asp.net Web应用程序,在随便一个xxx.aspx页面,右键"浏览方式"即可.. (2)通过修改项目属性也可以,右键…

一站式做网站网站发外链

故障现象&#xff1a;客户某台WINDOWS服务器掉电&#xff0c;ORACLE数据库STARTUP提示控制文件CONTROL01.CTL、CONTROL02.CTL被破坏。一、处理控制文件异常故障方法&#xff1a;直接拷贝CONTROL0一、处理控制文件异常故障二、尝试启动SQL> startup;ORACLE例程已经启动。Tota…

网站内容怎么编辑四种常用的erp软件

点击查看最新在线IDE流行度最新排名&#xff08;每月更新&#xff09; 2024年04月在线IDE流行度最新排名 TOP 在线IDE排名是通过分析在线ide名称在谷歌上被搜索的频率而创建的 在线IDE被搜索的次数越多&#xff0c;人们就会认为它越受欢迎。原始数据来自谷歌Trends 如果您相…

秦皇岛市城乡建设网站seo优化资源

在 Ubuntu 上安装 MySQL 的步骤如下&#xff1a; 更新系统软件包列表&#xff1a; sudo apt update 安装 MySQL 服务器&#xff1a; sudo apt install mysql-server 安装完成&#xff0c;可以使用以下命令检查 MySQL 服务器是否正在运行: sudo systemctl status mysql 如果 MyS…

淄博企业网站做数学网站

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/c43a0d72d29941c1b65c857d8ac9047e 思路 直接模拟参考答案Java import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返回方法规定的值…