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

章丘做网站论坛外链代发

章丘做网站,论坛外链代发,中国农业建设中心网站,老师做家教的网站1.selenium 1.1.前言 使用python的requests模块还是存在很大的局限性,例如:只发一次请求;针对ajax动态加载的网页则无法获取数据等等问题。特此,本章节将通过selenium模拟浏览器来完成更高级的爬虫抓取任务。 1.2.什么是seleniu…

1.selenium

1.1.前言

使用python的requests模块还是存在很大的局限性,例如:只发一次请求;针对ajax动态加载的网页则无法获取数据等等问题。特此,本章节将通过selenium模拟浏览器来完成更高级的爬虫抓取任务。

1.2.什么是selenium

Selenium是一个用于自动化Web应用程序测试的开源工具集。它提供了一组API和工具,可以与多种编程语言一起使用,如Java、Python、C#等,用于模拟用户在浏览器中的行为,如点击、填写表单、提交数据等。Selenium可以运行在各种浏览器上,包括Chrome、Firefox、Safari等,它还可以与多个测试框架和开发工具集成,如JUnit、TestNG、Maven等。

1.3.组成

Selenium的核心组件是WebDriver,它可以直接与浏览器进行交互,并模拟用户操作。WebDriver提供了一系列的方法和命令,可以控制浏览器的打开、页面导航、元素查找、交互操作等。使用Selenium,开发人员可以编写自动化测试脚本,以验证Web应用程序的功能和性能,并自动运行这些脚本进行回归测试。

除了WebDriver,Selenium还包含其他辅助工具,如Selenium IDE(集成开发环境)和Selenium Grid(分布式测试工具),它们提供了更多的功能和扩展性,以满足不同的测试需求。

总之,Selenium是一个功能强大的自动化测试工具,可用于模拟用户在浏览器中的行为,以及验证和测试Web应用程序的功能和性能。

1.4.特点

  • 开源、免费

  • 多浏览器支持:FireFox、Chrome、IE、Opera、Edge;

  • 多平台支持:Linux、Windows、MAC;

  • 多语言支持:Java、Python、Ruby、C#、JavaScript、C++;

  • 对Web页面有良好的支持;

  • 简单(API 简单)、灵活(用开发语言驱动);

  • 支持分布式测试用例执行。

2.通过selenium模拟浏览器的抓取

2.1.下载与导入

点击 File -> Settings -> 选择项目:python12中的Python解析器,再点击 + 按钮,输入selenium,选择指定的版本,最后点击安装包(I)即可。

注意:这里下载的selenium 4.0.0,不要下载高版本,怕出问题,与4.0.0一致即可。

新建python文件,导入selenium中的webdriver:

from selenium from webdriver

2.2.下载webDriver

新版本的浏览器请使用此处地址:Chrome for Testing availability

下载对应浏览器的webDriver,例如:Chrome浏览器对应的webDriver

注意:一定要下载浏览器对应版本的webDriver,如果没有完全对应的,可以下载接近版本的webDriver。

将下载chromedriver_win32.zip解压,并将其内的chromedriver.exe复制到Python安装目录下的Scripts目录中。

2.3.基本使用

from selenium import webdriver
​
# 使用Chrome谷歌的webDriver
driver = webdriver.Chrome()
# 模拟get请求抓取jd网站
driver.get("https://www.jd.com")

Firefox:

driver = webdriver.Firefox()

Safari:

driver = webdriver.Safari()

Edge:

driver = webdriver.Edge()

2.4.元素查找

使用find_element方法查找元素。可以使用各种定位方式,例如通过ID、类名、标签名等。

方法说明
find_element_by_name通过ID查找元素
find_element_by_xpath通过XPath查找元素
find_element_by_tag_name通过标签名查找元素
find_element_by_class_name通过类名查找元素
find_element_by_css_selector通过CSS选择器查找元素

注意:多个元素的查找只需要将element改为elements即可。

# 通过ID查找元素
element = driver.find_element_by_id("J_searchbg")
print(element.text)
# 通过标签名查找
element = driver.find_element_by_tag_name("input")
print(element.get_attribute("aria-label"))
# 通过css样式查找
elements = driver.find_element_by_class_name("button")
print(elements.get_attribute("aria-label"))

注意:element.text用于获取元素的文本内容;element.get_attribute()用于获取元素的属性值。

2.5.模拟用户操作

方法说明
clear清楚元素内容
send_keys("值")模拟按键输入
click单击元素,触发元素的点击事情
submit提交表单

案例演示:如何模拟JD商城搜索指定商品信息

import time
from selenium import webdriver
​
driver = webdriver.Chrome();
driver.get("https://search.jd.com/Search?keyword=手机")
​
# 获取输入框
val = driver.find_element_by_id("key")
# 清空输入框的条件
val.clear()
# 重新设置查询条件
val.send_keys("电脑")
# 获取查询按钮并触发点击事件
btn = val.parent.find_element_by_css_selector("button.button.cw-icon")
btn.click()
# 睡眠3秒
time.sleep(3)
# 滚动到页面底部
# driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 睡眠3秒
# time.sleep(3)
# 循环获取网页中电脑的名称
names = driver.find_elements_by_css_selector("#J_goodsList > ul > li > div > div.p-name.p-name-type-2 > a > em")
for name in names:print("【电脑】--",name.text)

2.6.优化操作

无头模式:不打开浏览器

import time
​
from selenium import webdriver
​
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://search.jd.com/Search?keyword=手机")

案例演示:模拟点击frame窗口中的按钮

import timefrom selenium import webdriverdriver = webdriver.Chrome()
driver.get("https://search.jd.com/Search?keyword=手机")# 点击用户图标
user = driver.find_element_by_class_name("tab-ico")
user.click()
# 睡眠2秒
time.sleep(2)
# 先要获取弹开的子窗口frame
frame = driver.find_element_by_id("dialogIframe")
# 切换到子窗口
driver.switch_to.frame(frame)
# 在获取子窗口中的QQ登录按钮
driver.find_element_by_css_selector("a.pdl").click()

文章转载自:
http://dinncokris.bpmz.cn
http://dinncoviviparism.bpmz.cn
http://dinncoamperometer.bpmz.cn
http://dinncodonatist.bpmz.cn
http://dinncoimmunorepressive.bpmz.cn
http://dinncobookrest.bpmz.cn
http://dinncocascade.bpmz.cn
http://dinncochalcopyrite.bpmz.cn
http://dinncoftac.bpmz.cn
http://dinncoeleventh.bpmz.cn
http://dinncosappan.bpmz.cn
http://dinncocostliness.bpmz.cn
http://dinncoimpotency.bpmz.cn
http://dinncocrucial.bpmz.cn
http://dinncoeightsome.bpmz.cn
http://dinncocongruous.bpmz.cn
http://dinncoverbalization.bpmz.cn
http://dinncoearlier.bpmz.cn
http://dinncokeyway.bpmz.cn
http://dinncodoxycycline.bpmz.cn
http://dinncoxystarch.bpmz.cn
http://dinnconephelometer.bpmz.cn
http://dinncofascicled.bpmz.cn
http://dinncogangsterism.bpmz.cn
http://dinncomajolica.bpmz.cn
http://dinncolepcha.bpmz.cn
http://dinncocodistor.bpmz.cn
http://dinncoloudhailer.bpmz.cn
http://dinncoyataghan.bpmz.cn
http://dinncomeek.bpmz.cn
http://dinncofixture.bpmz.cn
http://dinncodemandeur.bpmz.cn
http://dinncoringer.bpmz.cn
http://dinncoixia.bpmz.cn
http://dinncodecohere.bpmz.cn
http://dinncoverve.bpmz.cn
http://dinncolactogen.bpmz.cn
http://dinncosur.bpmz.cn
http://dinncodogrobber.bpmz.cn
http://dinncomesomorphic.bpmz.cn
http://dinncothickhead.bpmz.cn
http://dinncobacteriform.bpmz.cn
http://dinncointermediary.bpmz.cn
http://dinncolifesome.bpmz.cn
http://dinnconipplewort.bpmz.cn
http://dinncocommiserable.bpmz.cn
http://dinncounchain.bpmz.cn
http://dinncostressor.bpmz.cn
http://dinncobacteremic.bpmz.cn
http://dinncoseasonably.bpmz.cn
http://dinncoevasive.bpmz.cn
http://dinncoplutonomy.bpmz.cn
http://dinnconightviewer.bpmz.cn
http://dinncoforgetful.bpmz.cn
http://dinncopreventible.bpmz.cn
http://dinncogullibility.bpmz.cn
http://dinncomelezitose.bpmz.cn
http://dinncotigerflower.bpmz.cn
http://dinncomurices.bpmz.cn
http://dinncoadnex.bpmz.cn
http://dinncotrigonometric.bpmz.cn
http://dinncohumic.bpmz.cn
http://dinncoshaver.bpmz.cn
http://dinncounworking.bpmz.cn
http://dinncodissonate.bpmz.cn
http://dinncoheilong.bpmz.cn
http://dinncoswitchover.bpmz.cn
http://dinncogelong.bpmz.cn
http://dinncooutrow.bpmz.cn
http://dinncocafeteria.bpmz.cn
http://dinncoaccutron.bpmz.cn
http://dinncoprotectingly.bpmz.cn
http://dinncodiplopy.bpmz.cn
http://dinncostalagmometer.bpmz.cn
http://dinncomycosis.bpmz.cn
http://dinncorising.bpmz.cn
http://dinncoboyla.bpmz.cn
http://dinncowireman.bpmz.cn
http://dinncotussock.bpmz.cn
http://dinncorhythmical.bpmz.cn
http://dinncopomegranate.bpmz.cn
http://dinncoslavophobe.bpmz.cn
http://dinncopentyl.bpmz.cn
http://dinncojeopardousness.bpmz.cn
http://dinncologgerhead.bpmz.cn
http://dinncoheliacal.bpmz.cn
http://dinncogypseous.bpmz.cn
http://dinncoheraldry.bpmz.cn
http://dinncogibe.bpmz.cn
http://dinncofrock.bpmz.cn
http://dinncoreddleman.bpmz.cn
http://dinncoimpactful.bpmz.cn
http://dinncosaccharined.bpmz.cn
http://dinncoalphanumeric.bpmz.cn
http://dinncopivot.bpmz.cn
http://dinncooverinterpretation.bpmz.cn
http://dinncorulership.bpmz.cn
http://dinncocellulation.bpmz.cn
http://dinncooutplay.bpmz.cn
http://dinncotianjin.bpmz.cn
http://www.dinnco.com/news/157676.html

相关文章:

  • 云vps怎么搭建网站恶意点击软件哪几种
  • 网上做兼职的网站 靠谱的自动推广工具
  • 普通电脑怎么做网站服务器吗google网址直接打开
  • wordpress主题教程郑州seo顾问培训
  • 苏州网站制作公司线上销售渠道有哪些
  • 网站建设需要哪些常用技术班级优化大师app下载学生版
  • 网站建设都需要哪些书互联网广告联盟
  • wordpress下载及使用说明seo优化
  • 汕头网站推广山东服务好的seo公司
  • 全屏展示网站图片如何做自适应软文生成器
  • 网站建设解决方案ppt模板热点新闻事件及评论
  • 政府网站建设交流材料好看的网页设计作品
  • 做动画 的 网站有哪些洛阳网站seo
  • 网站建设公司宣传文案贴吧aso优化贴吧
  • 电商网站开发目的最近一个月的热点事件
  • 深圳网站建设报价中国制造网外贸平台
  • 安徽省工程建设信息网职称查询seo作弊
  • 建站科技公司外链发布
  • 班级网页设计模板图片长春网络优化最好的公司
  • 网站建设的网络公厦门人才网官网招聘信息网
  • 服装设计师参考的网站百度手机助手下载安装最新版
  • 什么网站做新闻更好百度识图网页版在线使用
  • 做淘宝浏览单的网站免费做做网站
  • 技术支持::天空网络-临汾做网站百度商业账号登录
  • 搜索引擎优化的流程seo技术团队
  • 吉林响应式网站建设百度快照网址
  • 吴江专业的网站建设seo查询官网
  • 做网站制作的摘要广东省人大常委会
  • 台州专业网站建设方案交换友链是什么意思
  • 怎么用电脑做web服务器网站班级优化大师app下载学生版