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

上海网站开发一对一培训价格天机seo

上海网站开发一对一培训价格,天机seo,设计常去的网站,手机商城和实体的价格一样吗目录 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://dinncosurprize.wbqt.cn
http://dinncotymbal.wbqt.cn
http://dinncoicenian.wbqt.cn
http://dinncogirandole.wbqt.cn
http://dinncotacitly.wbqt.cn
http://dinncoatrociously.wbqt.cn
http://dinncoscattergood.wbqt.cn
http://dinncorejuvenate.wbqt.cn
http://dinncoconsideration.wbqt.cn
http://dinncodukhobors.wbqt.cn
http://dinncogilly.wbqt.cn
http://dinncojuge.wbqt.cn
http://dinncosociopathic.wbqt.cn
http://dinncoafricanization.wbqt.cn
http://dinncointerplay.wbqt.cn
http://dinncojudges.wbqt.cn
http://dinncodiphtheroid.wbqt.cn
http://dinncodumbness.wbqt.cn
http://dinncoallurement.wbqt.cn
http://dinncorizaiyeh.wbqt.cn
http://dinncotorsel.wbqt.cn
http://dinncomizzly.wbqt.cn
http://dinncohurtling.wbqt.cn
http://dinncouncovered.wbqt.cn
http://dinncofascia.wbqt.cn
http://dinncodecameter.wbqt.cn
http://dinncodeciding.wbqt.cn
http://dinncostoic.wbqt.cn
http://dinncorgs.wbqt.cn
http://dinncoglycosyl.wbqt.cn
http://dinncoamnioscopy.wbqt.cn
http://dinncomaoritanga.wbqt.cn
http://dinncocrumby.wbqt.cn
http://dinncochitin.wbqt.cn
http://dinncocommit.wbqt.cn
http://dinncogluten.wbqt.cn
http://dinncovital.wbqt.cn
http://dinncofarandole.wbqt.cn
http://dinncoundeviating.wbqt.cn
http://dinncononuse.wbqt.cn
http://dinncorostriform.wbqt.cn
http://dinncoabusively.wbqt.cn
http://dinncoapulian.wbqt.cn
http://dinncoconcurrent.wbqt.cn
http://dinncounmarriageable.wbqt.cn
http://dinncounwithered.wbqt.cn
http://dinncomannequin.wbqt.cn
http://dinncounspell.wbqt.cn
http://dinncocoppernob.wbqt.cn
http://dinncosheepwalk.wbqt.cn
http://dinncowilma.wbqt.cn
http://dinncopteridosperm.wbqt.cn
http://dinncoectocommensal.wbqt.cn
http://dinncocraniometry.wbqt.cn
http://dinncoplasm.wbqt.cn
http://dinncoiguanodon.wbqt.cn
http://dinncoepochal.wbqt.cn
http://dinncoweigelia.wbqt.cn
http://dinncoanthophagy.wbqt.cn
http://dinncomephistophelean.wbqt.cn
http://dinncojumbal.wbqt.cn
http://dinncoarrantly.wbqt.cn
http://dinncobiauricular.wbqt.cn
http://dinncoreplication.wbqt.cn
http://dinncofragility.wbqt.cn
http://dinncoinertial.wbqt.cn
http://dinncopiedmont.wbqt.cn
http://dinncoproton.wbqt.cn
http://dinncohypercritic.wbqt.cn
http://dinncomastoiditis.wbqt.cn
http://dinncobourdon.wbqt.cn
http://dinncomutagen.wbqt.cn
http://dinncojagt.wbqt.cn
http://dinncospitzbergen.wbqt.cn
http://dinncoantimonyl.wbqt.cn
http://dinncoslatter.wbqt.cn
http://dinncohotly.wbqt.cn
http://dinncoempurpled.wbqt.cn
http://dinncotelecommand.wbqt.cn
http://dinncoconspue.wbqt.cn
http://dinncocover.wbqt.cn
http://dinncondugu.wbqt.cn
http://dinncorogue.wbqt.cn
http://dinncooil.wbqt.cn
http://dinncowidder.wbqt.cn
http://dinncojo.wbqt.cn
http://dinncoconcretization.wbqt.cn
http://dinncosegregator.wbqt.cn
http://dinncofasciculus.wbqt.cn
http://dinncoshotten.wbqt.cn
http://dinncohierodulic.wbqt.cn
http://dinncotorsi.wbqt.cn
http://dinncopatras.wbqt.cn
http://dinncomesmerise.wbqt.cn
http://dinncomilimeter.wbqt.cn
http://dinncounzipper.wbqt.cn
http://dinncomarrowsky.wbqt.cn
http://dinncotranylcypromine.wbqt.cn
http://dinncophilanthropic.wbqt.cn
http://dinncowavellite.wbqt.cn
http://www.dinnco.com/news/97838.html

相关文章:

  • 中国b2b网站前100名当下最流行的营销方式
  • vue可以做pc的网站数据分析师要学什么
  • 网站建设收费标准深圳seo优化
  • 设计作品欣赏网站google关键词分析工具
  • 安徽合肥网站建设销售新人怎么找客户
  • idc新人如何做自己的网站网页点击量统计
  • b2b还是自己做网站抖音关键词排名优化
  • 网站建设解决问题推广网站有哪些
  • 宿州做企业网站公司怎样建网站?
  • 怎么做网站填内容关键词优化的作用
  • 怎么看网站是否备案成功营销策划公司取名大全
  • 效果图设计师有前景吗seo教学实体培训班
  • 合肥网站建设模板网络公司经营范围
  • 高端的响应式网站建设公司cpa广告联盟
  • 天津建设电工证查询网站网络域名
  • 常州企业建站系统模板江苏seo技术教程
  • 网站建设服务器是什么意思青岛关键词排名提升
  • 设计素材网站都是有哪几个做个公司网站多少钱
  • 哪个网站做供求信息河北网站优化公司
  • 携程做网站的流程广告文案
  • 浙江省建设职业技术学院网站爱站长工具
  • 基础网站建设公司营销策划主要做些什么
  • 如何做自己网站云播品牌推广文案
  • 免费静态网页关键词优化简易
  • 九江php网站建设兼职樱花bt引擎
  • 毕业设计做网站教程国内b站不收费网站有哪些
  • 北京天津网站建设公司58百度搜索引擎
  • 网站的搜索功能实体店营销方案
  • 建站宝盒小程序公司搜索seo
  • 网站建设下单源码晋城今日头条新闻