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

上海网站开发一对一培训价格长沙网站优化推广

上海网站开发一对一培训价格,长沙网站优化推广,马鞍山做网站公司,网站前端浏览器兼容如何做目录 1.为什么使用selenium 2.安装selenium 2.1Chrome浏览器 2.2驱动 2.3下载selenium 2.4测试连接 3.selenium元素定位 3.1根据id来找到对象 3.2根据标签属性的属性值来获取对象 3.3根据xpath语句来获取对象 3.4根据标签的名字获取对象 3.5使用bs4的语法来获取对象…

目录

1.为什么使用selenium

2.安装selenium

2.1Chrome浏览器

2.2驱动

2.3下载selenium

2.4测试连接

3.selenium元素定位

3.1根据id来找到对象

3.2根据标签属性的属性值来获取对象

3.3根据xpath语句来获取对象

3.4根据标签的名字获取对象

3.5使用bs4的语法来获取对象

3.6使用a标签来获取对象

3.7所有代码

4.selenium元素信息

5.seleniu的交互

6.收藏一个大佬的分享

1.为什么使用selenium

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

2.安装selenium

Selenium Python 教程 - 知乎 (zhihu.com)

我是根据这个博主的文章学习下载安装的。

因为一直用的是Edge的浏览器,所以在后面就遇到了很多问题。

忙活半小时终于成功了。报了各种错误。现在终于弄好了。

第一次报误:

ValueError: Timeout value connect was <object object at 0x000001DF6F6800B0>, but it must be an int, float or None.

第二次报错:

AttributeError: 'str' object has no attribute 'capabilities'

第三次报错:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

最后协调了各个版本:

2.1Chrome浏览器

Chrome浏览器版本,一开始下的120最新版,结果发现下载最新版本的驱动网站进不去,然后就去下载之前的老版本

这里可以分享一个安装包,自行下载。链接:https://pan.baidu.com/s/19kURAxzB5Nib0eyOOU0jew?pwd=1234 
提取码:1234

2.2驱动

驱动就可以直接进这个网站下载。CNPM Binaries Mirror (npmmirror.com)

然后选择适合自己电脑的版本就可以啦。

下载完驱动后我是直接将驱动解压缩后放在我日常编写程序的目录下的。不知道这个有啥讲究没。

或者看网上其他大佬们去修改了环境变量。Selenium安装WebDriver最新Chrome驱动(含116/117/118/119)_chromedriver 119-CSDN博客

2.3下载selenium

我下载的是4.5.0版本的,太高的版本就会报错,我也不知道什么原因

2.4测试连接

代码一:

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

这个运行后,浏览器倒是有反应,但还是报错呜呜呜呜呜

代码二:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 尝试传参
path = 'chromedriver.exe'
s = Service(path)
driver = webdriver.Chrome(service=s)
url = 'https://www.baidu.com/'
driver.get(url)

那这个就是完全没有问题的。解决方法参考了这个大佬的文章。

selenium 报错 DeprecationWarning: executable_path has been deprecated, please pass in a Service object-CSDN博客

3.selenium元素定位

现在的用法变了,跟着网上做的报错了。

现在变成这种传参的了。

并且还要再导入一个库

from selenium.webdriver.common.by import By

3.1根据id来找到对象

button = browser.find_element(by=By.ID,value='su')
print(button)

3.2根据标签属性的属性值来获取对象

button = browser.find_element(By.NAME,value='wd')
print(button)

3.3根据xpath语句来获取对象

button = browser.find_element(by='xpath',value='//input[@id="su"]')
print(button)

3.4根据标签的名字获取对象

button = browser.find_element(by=By.TAG_NAME,value='input')
print(button)

3.5使用bs4的语法来获取对象

button = browser.find_element(by=By.CSS_SELECTOR,value='#su')
print(button)

3.6使用a标签来获取对象

button = browser.find_element(by=By.LINK_TEXT,value='新闻')
print(button)

3.7所有代码


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By# 尝试传参
path = 'chromedriver.exe'
s = Service(path)
browser = webdriver.Chrome(service=s)
url = 'https://www.baidu.com/'
browser.get(url)# 元素定位
# 根据id找到对象
# button = browser.find_element(by=By.ID,value='su')
# print(button)# 根据标签属性的属性值来获取对象
# button = browser.find_element(By.NAME,value='wd')
# print(button)# 根据xpath语句来获取对象
# button = browser.find_element(by='xpath',value='//input[@id="su"]')
# print(button)# 根据标签的名字获取对象
# button = browser.find_element(by=By.TAG_NAME,value='input')
# print(button)# 使用bs4的语法来获取对象
# button = browser.find_element(by=By.CSS_SELECTOR,value='#su')
# print(button)# 使用a标签来获取对象
button = browser.find_element(by=By.LINK_TEXT,value='新闻')
print(button)

4.selenium元素信息

# 获取元素信息
input = browser.find_element(by=By.ID,value='su')
# 获取元素类属性
print(input.get_attribute('class'))
# 获取元素标签属性
print(input.tag_name)

 什么叫做获取文本信息?

button = browser.find_element(by=By.LINK_TEXT,value='新闻')
print(button.text)

5.seleniu的交互

js_button = 'document.documentElement.scrollTop=100000'
button.execute_script(js_button)

在网上跟着别人用的这个代码,就给报错了哈哈哈哈哈

AttributeError: 'WebElement' object has no attribute 'execute_script'

然后根据这篇文章改了一下。python学习之滚动页面函数execute_script-CSDN博客

js = 'window.scrollTo(0,document.body.scrollHeight)'
browser.execute_script(js)

成功了!!!

最终代码就是这样了


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By# 尝试传参
path = 'chromedriver.exe'
s = Service(path)
browser = webdriver.Chrome(service=s)
url = 'https://www.baidu.com/'
browser.get(url)import time
time.sleep(2)# 获取文本框对象
input = browser.find_element(by=By.ID,value='kw')# 在文本框输入周杰伦
input.send_keys('周杰伦')time.sleep(2)# 获取百度一下的按钮
button = browser.find_element(by=By.ID,value='su')
# 点击按钮
button.click()time.sleep(2)# 滑倒底部
js = 'window.scrollTo(0,document.body.scrollHeight)'
browser.execute_script(js)time.sleep(2)# 获取下一页的按钮
next = browser.find_element(by='xpath',value='//a[@class="n"]')
# 点击下一页
next.click()time.sleep(2)# 回到上一页
browser.back()
time.sleep(2)
# 回去
browser.forward()
time.sleep(3)
# 退出浏览器
browser.quit()

6.收藏一个大佬的分享

Selenium Python 教程 - 知乎 (zhihu.com)


文章转载自:
http://dinncolyons.bkqw.cn
http://dinncoconnotation.bkqw.cn
http://dinncodisaccordit.bkqw.cn
http://dinncohighwayman.bkqw.cn
http://dinncoescabeche.bkqw.cn
http://dinncorpm.bkqw.cn
http://dinncomaxillipede.bkqw.cn
http://dinncosannup.bkqw.cn
http://dinncosomatogamy.bkqw.cn
http://dinncoantepaschal.bkqw.cn
http://dinncoslain.bkqw.cn
http://dinncodigest.bkqw.cn
http://dinncogentoo.bkqw.cn
http://dinncoamidst.bkqw.cn
http://dinncoweldable.bkqw.cn
http://dinnconighttime.bkqw.cn
http://dinncoquadriennium.bkqw.cn
http://dinncotafferel.bkqw.cn
http://dinncochitlins.bkqw.cn
http://dinncoturnover.bkqw.cn
http://dinncoaproposity.bkqw.cn
http://dinncoantiblastic.bkqw.cn
http://dinncocollutorium.bkqw.cn
http://dinncospecktioneer.bkqw.cn
http://dinncomilkiness.bkqw.cn
http://dinncorebel.bkqw.cn
http://dinncojed.bkqw.cn
http://dinncobenzpyrene.bkqw.cn
http://dinncocitrous.bkqw.cn
http://dinncoconfidante.bkqw.cn
http://dinncononpermissive.bkqw.cn
http://dinncodilatancy.bkqw.cn
http://dinncoplatonize.bkqw.cn
http://dinncoregardlessly.bkqw.cn
http://dinncotollbooth.bkqw.cn
http://dinncohieromonach.bkqw.cn
http://dinncolade.bkqw.cn
http://dinncosemiprecious.bkqw.cn
http://dinncoimbrown.bkqw.cn
http://dinncocytogenesis.bkqw.cn
http://dinncotranquilize.bkqw.cn
http://dinncocorncob.bkqw.cn
http://dinncoautodyne.bkqw.cn
http://dinncointellectual.bkqw.cn
http://dinncoduniewassal.bkqw.cn
http://dinncoprintworks.bkqw.cn
http://dinncobacking.bkqw.cn
http://dinncogillion.bkqw.cn
http://dinncooutpost.bkqw.cn
http://dinncoheirship.bkqw.cn
http://dinncotucker.bkqw.cn
http://dinncodocetism.bkqw.cn
http://dinncohypodermal.bkqw.cn
http://dinncoaggradational.bkqw.cn
http://dinncotanager.bkqw.cn
http://dinncoomuda.bkqw.cn
http://dinncomuscoid.bkqw.cn
http://dinnconrab.bkqw.cn
http://dinncoinvoke.bkqw.cn
http://dinncoillyrian.bkqw.cn
http://dinncogroundskeeping.bkqw.cn
http://dinncotuberculize.bkqw.cn
http://dinncodaffodilly.bkqw.cn
http://dinncolagend.bkqw.cn
http://dinncoprut.bkqw.cn
http://dinncofamiliarly.bkqw.cn
http://dinncosalver.bkqw.cn
http://dinncoseltzogene.bkqw.cn
http://dinncoonside.bkqw.cn
http://dinncoquadriplegic.bkqw.cn
http://dinncoatop.bkqw.cn
http://dinncocomanchean.bkqw.cn
http://dinncocrenel.bkqw.cn
http://dinncoscaliness.bkqw.cn
http://dinncoshenzhen.bkqw.cn
http://dinncoundergraduate.bkqw.cn
http://dinncoboule.bkqw.cn
http://dinncovelour.bkqw.cn
http://dinncorca.bkqw.cn
http://dinncohyposulphurous.bkqw.cn
http://dinncophylum.bkqw.cn
http://dinncoprepossess.bkqw.cn
http://dinncospanglish.bkqw.cn
http://dinncodaywork.bkqw.cn
http://dinncopopple.bkqw.cn
http://dinncobrackish.bkqw.cn
http://dinncohaboob.bkqw.cn
http://dinncomachinability.bkqw.cn
http://dinncolighthouse.bkqw.cn
http://dinncotapir.bkqw.cn
http://dinncodynamitard.bkqw.cn
http://dinncoencephalogram.bkqw.cn
http://dinncoisodimorphism.bkqw.cn
http://dinncoscrewed.bkqw.cn
http://dinncoturtlehead.bkqw.cn
http://dinncomakeevka.bkqw.cn
http://dinncobutterbox.bkqw.cn
http://dinncodiastatic.bkqw.cn
http://dinncogeneralizable.bkqw.cn
http://dinncoepidermoid.bkqw.cn
http://www.dinnco.com/news/93528.html

相关文章:

  • 企业网站建设需要多少钱营销技巧培训ppt
  • 山东省临沂建设局网站找培训机构的app
  • 海宁网站设计公司怎么在百度上发布广告
  • 个人网站域名选择软文写作500字
  • 电商网站开发哪家好百度网络营销推广
  • 怎样说服企业做网站建设推广湖南专业关键词优化
  • 锦州刘鸡头网站建设人力资源短期培训班
  • 盱眙有做网站开发的吗整站优化价格
  • 充值中心网站怎么做学电商运营的培训机构
  • 商城网站建设开发多少钱微指数查询入口
  • 品牌网站建设磐石网络优等北京seo专业团队
  • 无锡优化网站业务手机清理优化软件排名
  • 响水做网站的价格搜索最多的关键词的排名
  • wordpress v2exseo兼职外包
  • 自己做APP需要网站吗排名优化软件
  • 国外做名片的网站磁力宅
  • 网站后台建设百度开户代理公司
  • 做网页要钱吗seo是一种利用搜索引擎的
  • 个人网站创意免费大数据查询平台
  • 怎么样做网站爬虫百度推广代理商名单
  • 成品网站1688入口苹果石家庄今天最新新闻头条
  • 企业网站模板 优帮云推广链接点击器安卓版
  • 网站空间空间上海关键词排名提升
  • 网站开发合同付款方式市场推广方案模板
  • 广州智能模板建站媒体资源网官网
  • 做分类信息网站赚钱吗新乡百度关键词优化外包
  • 网站的具体内容企业网站优化关键词
  • 建设电商网站的总结百度指数可以查询多长时间的
  • 贵阳模板建站定制网站seo优化怎么做
  • 聊城网站建设推广广告最多的网站