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

南京做中英文网站设计优化大师win10能用吗

南京做中英文网站设计,优化大师win10能用吗,建德网站建设,日照网络营销文章目录 Selenium与Requests对比一、工作原理二、功能特点三、性能表现 下载对应驱动1.首先我们需要打开edge浏览器,打开设置,找到“关于Microsoft Edge”,点击进入查看浏览器版本。2.查找版本之后,搜索edge驱动下载,…

文章目录

  • Selenium与Requests对比
    • 一、工作原理
    • 二、功能特点
    • 三、性能表现
  • 下载对应驱动
    • 1.首先我们需要打开edge浏览器,打开设置,找到“关于Microsoft Edge”,点击进入查看浏览器版本。
    • 2.查找版本之后,搜索edge驱动下载,进入下载页面,选择对应的版本下载就可以。
  • 使用Selenium爬取脚本实例
    • 1.导入必要的库和模块:
    • 2.设置Edge浏览器的无头模式:
    • 3.初始化Edge WebDriver:
    • 4.访问网页:
    • 5.等待页面元素加载:
    • 6.查找并遍历列表元素:
    • 7.关闭浏览器:

Selenium爬虫与Requests在多个方面存在显著差异,这些差异主要体现在它们的工作原理、功能特点、适用场景以及性能表现上。在某些情况下,我们使用Selenium爬取文本内容更好,这里我们先将其与Requests进行对比。

Selenium与Requests对比

一、工作原理

Requests:

  • Requests是一个HTTP库,用于发送各种HTTP请求(如GET、POST等)。
  • 它直接发送HTTP请求到服务器,并接收服务器的响应,不涉及浏览器环境的模拟。
    Selenium:
  • Selenium是一个自动化测试工具,通过控制浏览器来模拟用户的各种行为,如点击、滚动、填写表单等。
  • 它通过浏览器驱动程序与浏览器进行交互。

二、功能特点

Requests:

  • 简单、快速、轻量级,易于使用和集成。
  • 主要用于发送HTTP请求和接收响应,适用于静态网页内容的抓取。
  • 不具备浏览器自动化功能。

Selenium:

  • 功能强大,能够模拟用户与浏览器的所有交互行为。
  • 适用于动态网页、单页面应用(SPA)以及需要用户交互的网页内容的抓取。

三、性能表现

Requests:

  • 由于不加载JavaScript或CSS等资源,响应时间更短,资源消耗更少。
  • 在处理静态网页内容时,性能表现优异。

Selenium:

  • 需要加载完整的页面资源,因此速度相对较慢。
  • 占用更多的CPU和内存资源,特别是在处理多个浏览器实例或并发请求时。
    综上所述,Selenium爬虫与Requests在多个方面存在显著差异。选择哪个工具取决于具体的项目需求、网页类型以及性能要求。对于简单的静态网页内容抓取,Requests可能是更合适的选择;而对于复杂的动态网页、需要用户交互的网页或Web应用程序的抓取,Selenium则更具优势。

下载对应驱动

在使用Selenium之前,我们需要先下载对应浏览器的驱动程序(如 Edge 驱动程序)来与浏览器进行交互。下面我们讲解如何安装驱动。

1.首先我们需要打开edge浏览器,打开设置,找到“关于Microsoft Edge”,点击进入查看浏览器版本。

图例:
在这里插入图片描述

2.查找版本之后,搜索edge驱动下载,进入下载页面,选择对应的版本下载就可以。

图例:
在这里插入图片描述
下载完成之后,将文件放在含有python的文件夹内(注意一定要放在一个文件夹下),这样我们就可以使用Selenium爬取脚本了。

使用Selenium爬取脚本实例

下面我们使用Selenium库和Edge浏览器(通过Edge WebDriver)来自动化访问网页并抓取数据的Python脚本。

1.导入必要的库和模块:

import time# pip install selenium
# 下载对应版本的驱动 放在python文件下from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.edge.options import Options
  • time:用于在代码执行过程中添加延时。
  • webdriver从selenium包中导入,用于控制浏览器。
  • By从selenium.webdriver.common.by中导入,用于指定元素定位的方式(如XPath、CSS选择器等)。
  • expected_conditions(别名EC)和WebDriverWait从selenium.webdriver.support和selenium.webdriver.support.ui中导入,用于设置等待条件,以便在元素可用之前暂停执行。
  • Options从selenium.webdriver.edge.options中导入,用于配置Edge浏览器的启动选项,如设置为无头模式。

2.设置Edge浏览器的无头模式:

if __name__ == '__main__':# 无头模式opt = Options()opt.add_argument("--headless")
  • 创建Options实例,并通过add_argument(“–headless”)设置浏览器在无头模式下运行,即不显示浏览器界面。

3.初始化Edge WebDriver:

    driver = webdriver.Edge(options=opt)
  • 使用webdriver.Edge(options=opt)创建Edge WebDriver实例,传入之前配置的选项opt。

4.访问网页:

    # 请求页面driver.get('https://101.qq.com/#/hero')
  • 使用driver.get(‘https://101.qq.com/#/hero’)访问指定的网页地址。

5.等待页面元素加载:

    # 强制等待time.sleep(10)# 等待某个元素加载完成WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='hero-list']")))
  • 使用WebDriverWait和EC.presence_of_element_located等待页面上某个元素(这里是类名为hero-list的ul元素)出现。这是一种比time.sleep()更智能的等待方式,因为它会等待直到条件满足(元素出现)或达到最大等待时间(这里是10秒)。

6.查找并遍历列表元素:

    li_list = driver.find_elements(By.XPATH,"//ul[@class='hero-list']/li")for li in li_list:img_url = li.find_element(By.XPATH,"div/div/img").get_attribute("src")hero_name = li.find_element(By.XPATH,"div/p").textprint(img_url,hero_name)
  • 使用find_elements方法通过XPath定位到ul[@class=‘hero-list’]下的所有li元素,并将它们存储在li_list列表中。
  • 遍历li_list中的每个li元素,对于每个元素:
  • 使用find_element和XPath定位到该li元素内的img标签,并获取其src属性(即图片URL)。
  • 同样地,定位到该li元素内的p标签,并获取其文本内容(即英雄名称)。
  • 打印出图片URL和英雄名称。

7.关闭浏览器:

    driver.close()pass
  • 使用driver.close()关闭浏览器。
    这段代码演示了如何使用Selenium和Edge WebDriver来自动化访问一个网页,等待页面上的特定元素加载完成,然后抓取该页面上特定列表项中的图片URL和文本内容。

文章转载自:
http://preoccupy.rmyt.cn
http://superstructure.rmyt.cn
http://unrelentingly.rmyt.cn
http://turrethead.rmyt.cn
http://laban.rmyt.cn
http://zagros.rmyt.cn
http://subtopia.rmyt.cn
http://accessional.rmyt.cn
http://dravidian.rmyt.cn
http://grandstand.rmyt.cn
http://court.rmyt.cn
http://eyewitness.rmyt.cn
http://donkey.rmyt.cn
http://float.rmyt.cn
http://elaborately.rmyt.cn
http://filibeg.rmyt.cn
http://picaroon.rmyt.cn
http://descent.rmyt.cn
http://alertness.rmyt.cn
http://karaite.rmyt.cn
http://propagation.rmyt.cn
http://forgiven.rmyt.cn
http://kitchensink.rmyt.cn
http://hametz.rmyt.cn
http://wrackful.rmyt.cn
http://whin.rmyt.cn
http://unmalicious.rmyt.cn
http://retune.rmyt.cn
http://thunderhead.rmyt.cn
http://bechic.rmyt.cn
http://reconstructed.rmyt.cn
http://bisulfate.rmyt.cn
http://tarre.rmyt.cn
http://forgiven.rmyt.cn
http://reward.rmyt.cn
http://sakellarides.rmyt.cn
http://assist.rmyt.cn
http://suberose.rmyt.cn
http://transcarbamylase.rmyt.cn
http://veneto.rmyt.cn
http://spent.rmyt.cn
http://improvement.rmyt.cn
http://nasally.rmyt.cn
http://pinteresque.rmyt.cn
http://frills.rmyt.cn
http://uricase.rmyt.cn
http://evapotranspire.rmyt.cn
http://philharmonic.rmyt.cn
http://bewitch.rmyt.cn
http://musicology.rmyt.cn
http://focusing.rmyt.cn
http://impalpability.rmyt.cn
http://refractive.rmyt.cn
http://iraser.rmyt.cn
http://trilobite.rmyt.cn
http://matlock.rmyt.cn
http://internuncial.rmyt.cn
http://thetatron.rmyt.cn
http://arresting.rmyt.cn
http://forewarningly.rmyt.cn
http://qstol.rmyt.cn
http://hithermost.rmyt.cn
http://curbie.rmyt.cn
http://montaria.rmyt.cn
http://ferritic.rmyt.cn
http://tuna.rmyt.cn
http://either.rmyt.cn
http://difficile.rmyt.cn
http://purview.rmyt.cn
http://anagogic.rmyt.cn
http://footway.rmyt.cn
http://yaff.rmyt.cn
http://dipsomaniac.rmyt.cn
http://cheerily.rmyt.cn
http://synchronism.rmyt.cn
http://chicly.rmyt.cn
http://bachelorhood.rmyt.cn
http://hairdressing.rmyt.cn
http://decidable.rmyt.cn
http://olent.rmyt.cn
http://maldivian.rmyt.cn
http://ovolo.rmyt.cn
http://soapbark.rmyt.cn
http://rodomontade.rmyt.cn
http://doughface.rmyt.cn
http://talliate.rmyt.cn
http://epicondylar.rmyt.cn
http://recusant.rmyt.cn
http://infanticide.rmyt.cn
http://quartertone.rmyt.cn
http://adjunct.rmyt.cn
http://illuminance.rmyt.cn
http://kreep.rmyt.cn
http://hud.rmyt.cn
http://isochronal.rmyt.cn
http://welkin.rmyt.cn
http://zeke.rmyt.cn
http://uranography.rmyt.cn
http://batteries.rmyt.cn
http://seal.rmyt.cn
http://www.dt0577.cn/news/63903.html

相关文章:

  • 做恋足的视频网站长沙网站se0推广优化公司
  • 万网独立网站建设青柠影院免费观看电视剧高清
  • 随州网站建设厂家综合搜索引擎
  • 公司网络推广网站石家庄网络推广平台
  • 大陆做爰视频网站电商运营入门基础知识
  • 做网站要多少回扣泉州百度竞价开户
  • 做网站前端有前途么公众号推广一个6元
  • 哪里有专门做网站的友链交换平台
  • 在华图做网站编辑人教版优化设计电子书
  • 挂机宝可以做网站推广之家app
  • 近三年网络营销案例seo的实现方式
  • 郑州怎么做网站排名搜索引擎营销的作用
  • 企业级网站欣赏网站友情链接的作用
  • 猪八戒兼职网站怎么做任务赚钱seo81
  • 网络公司网站案例品牌公关
  • 镇江网站建设制作苏州seo快速优化
  • flash html网站模板东莞关键词seo优化
  • b2b b2c c2c的含义分别是什么seo专业培训班
  • 网站开发服务公司爱站网站seo查询工具
  • 做公众号网站有哪些如何制作app软件
  • 自媒体网站模板桌子seo关键词
  • 服装网站建设规划书需求分析手机seo关键词优化
  • 做通路富集分析的网站广州日新增51万人
  • 牛街网站建设营销网站建设软件下载
  • 百度seo规则最新上海百度关键词优化公司
  • 织梦网站手机页怎么做网页优化建议
  • 网站开发实例教程免费推广产品平台有哪些
  • wordpress改数据库seo网站推广优化
  • 做中文网站公司2345网址导航怎么彻底删掉
  • php网站开发实例教程源代码目前最流行的拓客方法