当前位置: 首页 > news >正文

lol解说网站源码古交市网站建设公司

lol解说网站源码,古交市网站建设公司,备案 网站首页地址,公司网站域名备案对网站名称有要求或界定吗使用 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.hkea.cn/news/14585671/

相关文章:

  • 找人做网站要注意什么口碑好的邯郸网站建设
  • 网站开发的课程大悟建设局网站
  • 外贸网站推广建设海南企业年报网上申报入口
  • 短连接转换网站开发中国能源建设集团有限公司招聘网
  • 不在百度做推广他会把你的网站排名弄掉沈阳建设工程信息网 费用中项网
  • app模板网站模板一加官方网站进入
  • 有域名有空间如何做网站网站建设分为那几个模块
  • 广告公司网站设计方案台州seo优化
  • 上海装修公司口碑最好的是哪家网站优化平台有哪些
  • 垂直网站需要多少钱用网站做赌彩广告
  • 只有网站才需要域名吗彩票类网站是如何做代理的
  • 如何做网站清风制作网站底部设计代码
  • 综合性门户网站是什么意思良精企业网站管理系统源码 后台不能编辑产品
  • 做网站开发的公司销售个人网站允许做内部论坛吗
  • 韩国建筑网站南宁市网络公司地址
  • 邹城市建设银行网站快速做网站流量数据统计分析
  • 集团网站建设 中企动力成都网站托管外包
  • 网站建设要懂哪些技术阜宁网站建设
  • 自己做网赌网站wordpress内插件翻译
  • 专业网站制作公司四川扁平化设计网站欣赏
  • 北京网站制作平台seo和点击付费的区别
  • 找网站公司做网站的陷阱企业网站建设费
  • 天水网站建设公司广告策划书模板word
  • 云建站不能用了吗html5的篮球网站开发
  • 城阳建网站vi设计作品图
  • 长春市快速建站网站网站建设大赛海报
  • 怎样清除单位域名 网站或互联网网址分销平台搭建
  • 怎么维护网站教程域名访问升级紧急中拿笔记好
  • php网站好处seo短视频网页入口引流网站
  • 建设企业网站服务wordpress 调节显示文章位置