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

网站建设费 开办费专业关键词排名软件

网站建设费 开办费,专业关键词排名软件,盘锦市网站建设,宝塔网站做301重定向目录 1. Selenium简介2. 为什么使用Selenium?3. Selenium的安装4. Selenium的使用5. Selenium的元素定位6. Selenium的交互7. Chrome handless参考文献 原文地址:https://program-park.top/2023/10/16/reptile_3/ 本文章中所有内容仅供学习交流使用&…

目录

  • 1. Selenium简介
  • 2. 为什么使用Selenium?
  • 3. Selenium的安装
  • 4. Selenium的使用
  • 5. Selenium的元素定位
  • 6. Selenium的交互
  • 7. Chrome handless
  • 参考文献

原文地址:https://program-park.top/2023/10/16/reptile_3/

本文章中所有内容仅供学习交流使用,不用于其他任何目的,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关。

1. Selenium简介

  Selenium 是一个用于 Web 应用程序测试的工具。最初是为网站自动化测试而开发的,可以直接运行在浏览器上,支持的浏览器包括 IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera 和 Edge 等。
  爬虫中使用它是为了解决 requests 无法直接执行 JavaScript 代码的问题。Selenium 本质上是通过驱动浏览器,彻底模拟浏览器的操作,好比跳转、输入、点击、下拉等,来拿到网页渲染之后的结果。Selenium 是 Python 的一个第三方库,对外提供的接口能够操作浏览器,从而让浏览器完成自动化的操作。

2. 为什么使用Selenium?

  Selenium 能模拟浏览器功能自动执行网页中的 JavaScript 代码,实现动态加载。

3. Selenium的安装

  谷歌浏览器驱动下载地址:https://registry.npmmirror.com/binary.html?path=chromedriver/
  查看自己谷歌浏览器的版本,我这里的版本是正式版本116.0.5845.188,驱动下载地址最新的只有114.0.5735.90,所以只能去官网的测试页面下载118.0.5993.70版本的驱动(https://googlechromelabs.github.io/chrome-for-testing/#stable,版本向下兼容),然后把下载的压缩包解压,将exe文件放入 PyCharm 项目的根目录下。
  之后执行pip install selenium命令,安装 selenium 库。

4. Selenium的使用

from selenium import webdriver# 创建浏览器操作对象
path = 'chromedriver.exe'
browser= webdriver.Chrome(path)# 访问网站
url = 'https://www.baidu.com'browser.get(url)
# content = browser.page_source
# print(content)

  需要注意的是,如果你的 selenium 是4.11.2以上的版本,不需要设置driver.exe的路径,selenium 可以自己处理浏览器的驱动程序,因此代码直接改为brower = webdriver.Chrome()即可。
  运行代码,得到下面的效果:

5. Selenium的元素定位

  自动化工具要做的就是模拟鼠标和键盘来操作点击、输入等等元素,但是操作这些元素的前提是找到它们,WebDriver 提供了很多元素定位的方法:

  • 根据标签 id 获取元素:
    from selenium import webdriver
    from selenium.webdriver.common.by import By# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser= webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)button = browser.find_element(By.ID, 'su')
    # button = browser.find_elements(By.ID, 'su')
    print(button)
    
  • 根据标签 name 属性的值获取元素:
    button = browser.find_element(By.NAME, 'wd')
    print(button)
    
  • 根据 Xpath 语句获取元素;
    button = browser.find_element(By.XPATH, '//input[@id="su"]')
    print(button)
    
  • 根据标签名获取元素:
    button = browser.find_elements(By.TAG_NAME, 'input')
    print(button)
    
  • 根据 bs4 语法获取元素:
    button = browser.find_elements(By.CSS_SELECTOR, '#su')
    print(button)
    
  • 根据标签的文本获取元素(精确定位):
    button = browser.find_elements(By.LINK_TEXT, '地图')
    print(button)
    
  • 根据标签的文本获取元素(模糊定位):
    button = browser.find_elements(By.PARTIAL_LINK_TEXT, '地')
    print(button)
    
  • 根据 class 属性获取元素:
    button = browser.find_element(By.CLASS_NAME, 'wrapper_new')
    print(button)
    

  当我们定位到元素之后,自然就要考虑如何获取到元素的各种信息,selenium 给我们提供了获取元素不同信息的方法:

  • 获取元素属性:
    from selenium import webdriver
    from selenium.webdriver.common.by import By# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser= webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)button = browser.find_element(By.ID, 'su')
    print(input.get_attribute('class'))
    
  • 获取元素标签名:
    input = browser.find_element(By.ID, 'su')
    print(input.tag_name)
    
  • 获取元素文本:
    input = browser.find_element(By.ID, 'su')
    print(input.text)
    
  • 获取元素位置:
    input = browser.find_element(By.ID, 'su')
    print(input.location)
    
  • 获取元素大小:
    input = browser.find_element(By.ID, 'su')
    print(input.size)
    

6. Selenium的交互

  页面交互指的是我们平时在浏览器上的各种操作,比如输入文本、点击链接、回车、下拉框等,下面就演示 selenium 是如何进行页面交互的。

  • 输入文本:
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser = webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)# 定位输入框
    input = browser.find_element(By.ID, 'kw')
    # 输入文本selenium
    input.send_keys('selenium')
    time.sleep(2)# 关闭浏览器
    browser.close()
    
  • 点击:
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser = webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)# 定位输入框
    input = browser.find_element(By.ID, 'kw')
    # 输入文本selenium
    input.send_keys('selenium')
    time.sleep(2)# 定位百度一下的按钮
    button = browser.find_element(By.ID, 'su')
    # 点击按钮
    button.click()
    time.sleep(2)# 关闭浏览器
    browser.close()
    
  • 清除文本:
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser = webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)# 定位输入框
    input = browser.find_element(By.ID, 'kw')
    # 输入文本selenium
    input.send_keys('selenium')
    time.sleep(2)# 清除selenium
    input.clear()
    time.sleep(2)# 关闭浏览器
    browser.close()
    
  • 回车确认:
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser = webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)# 定位输入框
    input = browser.find_element(By.ID, 'kw')
    # 输入文本selenium
    input.send_keys('selenium')
    time.sleep(2)# 回车查询
    input.submit()
    time.sleep(2)# 关闭浏览器
    browser.close()
    
  • 运行 JavaScript:
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser = webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)# 定位输入框
    input = browser.find_element(By.ID, 'kw')
    # 输入文本selenium
    input.send_keys('selenium')
    time.sleep(2)# 回车查询
    input.submit()
    time.sleep(2)# js代码
    js_bottom = 'document.documentElement.scrollTop=100000'
    # 下拉进度条,页面滑动
    browser.execute_script(js_bottom)
    time.sleep(2)# 关闭浏览器
    browser.close()
    
  • 前进后退
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time# 创建浏览器操作对象
    # path = 'chromedriver.exe'
    browser = webdriver.Chrome()# 访问网站
    url = 'https://www.baidu.com'
    browser.get(url)# 定位输入框
    input = browser.find_element(By.ID, 'kw')
    # 输入文本selenium
    input.send_keys('selenium')
    time.sleep(2)# 回车查询
    input.submit()
    time.sleep(2)# js代码
    js_bottom = 'document.documentElement.scrollTop=100000'
    # 页面滑动
    browser.execute_script(js_bottom)
    time.sleep(2)# 定位下一页的按钮
    next = browser.find_element(By.XPATH, '//a[@class="n"]')
    # 点击下一页
    next.click()
    time.sleep(2)# 返回到上一页面
    browser.back()
    time.sleep(2)# 前进到下一页
    browser.forward()
    time.sleep(2)# 关闭浏览器
    browser.close()
    

7. Chrome handless

  在上面的测试过程中可以发现,虽然 selenium 简便好用,但是它的运行速度很慢,这是因为 selenium 是有界面的,需要执行前端 css 和 js 的渲染。那么下面就介绍一个无界面的浏览器,Chrome-handless 模式,运行效率要比真实的浏览器快很多,在 selenium 的基础上,支持页面元素查找、js 执行等,代码和 selenium 一致。
  使用前提:

  • Chrome
    • Unix\Linux chrome >= 59
    • Windows chrome >= 60
  • Python >= 3.6
  • Selenium >= 3.4.*
from selenium import webdriverdef share_browser():# headless自带配置,不需要再做额外的修改from selenium.webdriver.chrome.options import Options# 初始化chrome_options = Options()chrome_options.add_argument('‐‐headless')chrome_options.add_argument('‐‐disable‐gpu')# 谷歌浏览器的安装路径path = r'C:\Users\\AppData\Local\Google\Chrome\Application\chrome.exe'chrome_options.binary_location = pathbrowser = webdriver.Chrome(options=chrome_options)return browserbrowser = share_browser()
url = 'https://www.baidu.com'
browser.get(url)# 本地保存照片
browser.save_screenshot('baidu.png')

参考文献

  【1】http://www.noobyard.com/article/p-boitcibx-g.html
  【2】https://www.jb51.net/article/149145.htm
  【3】https://zhuanlan.zhihu.com/p/462460461
  【4】https://blog.csdn.net/weixin_67553250/article/details/127555724
  【5】https://www.cnblogs.com/Summer-skr–blog/p/11491078.html
  【6】https://www.bilibili.com/video/BV1Db4y1m7Ho?p=77


文章转载自:
http://butylene.c7507.cn
http://immoderation.c7507.cn
http://finner.c7507.cn
http://granulocytopenia.c7507.cn
http://junkman.c7507.cn
http://decathlon.c7507.cn
http://vitallium.c7507.cn
http://algebraist.c7507.cn
http://ichthyologic.c7507.cn
http://kiwanis.c7507.cn
http://blithely.c7507.cn
http://decompresssion.c7507.cn
http://glucocorticoid.c7507.cn
http://scripter.c7507.cn
http://relent.c7507.cn
http://cellophane.c7507.cn
http://irbm.c7507.cn
http://achromatous.c7507.cn
http://unpenetrable.c7507.cn
http://physiology.c7507.cn
http://hetaira.c7507.cn
http://harmful.c7507.cn
http://retractation.c7507.cn
http://communications.c7507.cn
http://tehuantepec.c7507.cn
http://withdrawal.c7507.cn
http://unbreakable.c7507.cn
http://ballsy.c7507.cn
http://beryllium.c7507.cn
http://times.c7507.cn
http://collarless.c7507.cn
http://lousewort.c7507.cn
http://sodalist.c7507.cn
http://peracid.c7507.cn
http://mis.c7507.cn
http://tussock.c7507.cn
http://jacarta.c7507.cn
http://guttural.c7507.cn
http://mishear.c7507.cn
http://yird.c7507.cn
http://mobocracy.c7507.cn
http://nexus.c7507.cn
http://submergible.c7507.cn
http://weregild.c7507.cn
http://laconic.c7507.cn
http://unclench.c7507.cn
http://snug.c7507.cn
http://mangalore.c7507.cn
http://inexpedience.c7507.cn
http://rebounder.c7507.cn
http://nitrification.c7507.cn
http://violone.c7507.cn
http://calced.c7507.cn
http://tapis.c7507.cn
http://asquint.c7507.cn
http://nonsocial.c7507.cn
http://proclivity.c7507.cn
http://agronomic.c7507.cn
http://exheredate.c7507.cn
http://seigneur.c7507.cn
http://foreshadow.c7507.cn
http://topotype.c7507.cn
http://bandoeng.c7507.cn
http://thoracicolumbar.c7507.cn
http://din.c7507.cn
http://pomerania.c7507.cn
http://unpronounceable.c7507.cn
http://loquitur.c7507.cn
http://erasion.c7507.cn
http://monopolism.c7507.cn
http://ubiquitous.c7507.cn
http://viceregal.c7507.cn
http://devoted.c7507.cn
http://mesmerisation.c7507.cn
http://assignee.c7507.cn
http://cornstalk.c7507.cn
http://viewphone.c7507.cn
http://berhyme.c7507.cn
http://tempest.c7507.cn
http://seropositive.c7507.cn
http://grille.c7507.cn
http://towie.c7507.cn
http://douppioni.c7507.cn
http://journalese.c7507.cn
http://distil.c7507.cn
http://onr.c7507.cn
http://doffer.c7507.cn
http://blockhouse.c7507.cn
http://piscary.c7507.cn
http://kazatska.c7507.cn
http://bwr.c7507.cn
http://fireroom.c7507.cn
http://powwow.c7507.cn
http://stum.c7507.cn
http://jessie.c7507.cn
http://tormentor.c7507.cn
http://umpteen.c7507.cn
http://craneman.c7507.cn
http://unalleviated.c7507.cn
http://energise.c7507.cn
http://www.zhongyajixie.com/news/94667.html

相关文章:

  • 网站建设飠金手指科杰十五百度做广告推广怎么样
  • 怎么建立和设计网站电商代运营十大公司排名
  • 怎么拥有网站的所有权国际热点事件
  • 公司品牌flash网站磁力引擎
  • 网站建设公司资讯网站制作流程和方法
  • 电影网站建设教程下载2345网址导航电脑版
  • wordpress多站点无法发布文章seo优化网页
  • app开发公司哪里做官网排名优化
  • 建网站 铸品牌 做推广免费推广产品的网站
  • 哪个网站可以查蛋白互做微商软文
  • 买网站需要多少钱百度推广充值必须5000吗
  • 网站建设模板怎么用宁波seo推广推荐
  • 国家建设管理信息网站百度竞价排名服务
  • 网站建设高效解决之道晋中网站seo
  • 常州网站建设案例软文拟发布的平台与板块
  • wordpress 新手教程seo新人怎么发外链
  • 做网站要的软件清远网站seo
  • 网站怎么做跳转安全搜索点击软件
  • 自动化发布 iis网站站长网站推广
  • 特色网站模板软文营销网站
  • 计算机网络技术主修课程快速优化系统
  • 湖南响应式网站建设费用百度优化关键词
  • c .net 做网站网络营销成功的案例
  • 网站设计 佛山优化seo可以从以下几个方面进行
  • 网站开发系统国内免费域名
  • wordpress 注册体验百度网站怎么优化排名
  • 可以免费创建网站的软件google ads
  • 凡科主要是做什么的农大南路网络营销推广优化
  • 智能锁网站建设关键词seo快速优化技术
  • wordpress建外贸网站百度搜索网页