python splinter api整理
官方文档:Splinter — Splinter 0.10.0 documentation
安装:
pip install splinter
下载驱动(这里是Chrome驱动)
谷歌浏览器驱动地址: https://npm.taobao.org/mirrors/chromedriver/
选择自己浏览器版本对应的驱动,windows选win32下载就行,选错了驱动会报错哦
下载驱动后放入python的安装目录
windows安装目录:
从开始菜单中找到Anaconda或python => 右键 => 打开文件位置 => 弹出文件夹后 => 右键 => 打开文件位置
直接把驱动放到这个文件夹就行了
from splinter import Browser # 导入包
browser = Browser() # 创建一个实例
# if you don’t provide any driver to the Browser function, firefox will be used
# 如果没有提供任何浏览器驱动默认使用火狐浏览器驱动(需安装火狐浏览器)
或者这样使用,退出with自动关闭浏览器
from splinter import Browser
with Browser() as b:# stuff using the browser
使用其他驱动创建实例
browser = Browser('chrome')
browser = Browser('firefox')
browser = Browser('zope.testbrowser')
导航到某个URL
browser.visit('http://cobrateam.info')
浏览器窗口管理
# 所有打开的窗口
browser.windows # all open windows
# 第一个窗口
browser.windows[0] # the first window
# 名字为window_name值的窗口
browser.windows[window_name] # the window_name window
# 当前窗口
browser.windows.current # the current window
# 设置第三个窗口为当前窗口
browser.windows.current = browser.windows[3] # set current window to window 3window = browser.windows[0]
# 返回一个布尔值表示当前这个窗口对象是否是活动窗口
window.is_current # boolean - whether window is current active window
# 设置window为当前活动窗口
window.is_current = True # set this window to be current window
# 获取window的下一个窗口
window.next # the next window
# 获取window的上一个窗口
window.prev # the previous window
# 关闭这个窗口
window.close() # close this window
# 关闭其他窗口
window.close_others() # close all windows except this one
重新加载页面
browser.reload()
跳转历史
browser.visit('http://cobrateam.info')
browser.visit('https://splinter.readthedocs.io')
# 上一个历史记录
browser.back()
# 下一个历史记录
browser.forward()
网页内容操作
# You can get the title of the visited page using the title attribute:
browser.title
# You can use the html attribute to get the html content of the visited page:
browser.html
# The visited page’s url can be accessed by the url attribute:
browser.url
# You can pass a User-Agent header on Browser instantiation.
b = Browser(user_agent="Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)")
splinter 选择器提供了6个方法来定位页面元素,css, xpath, tag, name, id, value, text.
browser.find_by_css('h1')
browser.find_by_xpath('//h1')
browser.find_by_tag('h1')
browser.find_by_name('name')
browser.find_by_text('Hello World!')
# 返回一个元素,其他返回一个元素列表
browser.find_by_id('firstheader')
browser.find_by_value('query')
你可以通过fist、last、index的方式访问选择器返回的元素列表
# Each of these methods returns a list with the found elements.
# You can get the first found element with the first shortcut:
first_found = browser.find_by_name('name').first
# There’s also the last shortcut – obviously, it returns the last found element:
last_found = browser.find_by_name('name').last
# You also can use an index to get the desired element in the list of found elements:
second_found = browser.find_by_name('name')[1]
A web page should have only one id, so the find_by_id method returns always a list with just one element.
寻找超链接的一些方法
#根据描述,即<a>和</a>标签中间的内容,直接做内容匹配
links_found = browser.find_link_by_text('Link for Example.com')
# 不同于上面的这个是做正则匹配
links_found = browser.find_link_by_partial_text('for Example')
# 根据href属性做内容匹配
links_found = browser.find_link_by_href('http://example.com')
# 根据href属性做正则匹配
links_found = browser.find_link_by_partial_href('example')
If an element is not found, the find_ methods return an empty list. But if you try to access an element in this list, the method will raise the splinter.exceptions.ElementDoesNotExist exception.*
对定位到的一些元素的事件触发
# 触发某个元素的点击事件
browser.find_by_tag('h1').click()
# 双击事件
browser.find_by_tag('h1').double_click()
# 右键点击
browser.find_by_tag('h1').right_click()
you can drag an element and drop it to another element! The example below drags the <h1>…</h1> element and drop it to a >container element (identified by a CSS class).
draggable = browser.find_by_tag('h1')
target = browser.find_by_css('.container')
draggable.drag_and_drop(target)
获取元素的值
# method one
browser.find_by_css('h1').first.value
# method two
element = browser.find_by_css('h1').first
element.value
点击一个链接、按钮
# link
browser.click_link_by_href('http://www.the_site.com/my_link')
browser.click_link_by_partial_href('my_link')
browser.click_link_by_text('my link')
browser.click_link_by_partial_text('part of link text')
browser.click_link_by_id('link_id')
#button
browser.find_by_name('send').first.click()
browser.find_link_by_text('my link').first.click()
cookie操作
# 添加
browser.cookies.add({'whatever': 'and ever'})
# 获取所有cookie
browser.cookies.all()
# 删除
browser.cookies.delete('mwahahahaha') # deletes the cookie 'mwahahahaha'
# 删除多个
browser.cookies.delete('whatever', 'wherever') # deletes two cookies
# 删除所有
browser.cookies.delete() # deletes all cookies
在实际开发中经常需要等待页面的某些元素加载渲染完成后我们才能进行操作,这里给出一种比较常用的方法
from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait(browser, 20, 0.5).until(lambda driver:driver.find_link_by_href('https://i.csdn.net')
)# WebDriverWait:
# 第一个参数是驱动对象
# 第二个参数是最大超时时间
# 第三个参数是每过多久判断一次
# until方法(函数返回 真 时 结束等待,否则继续等待):
# 第一个参数需要一个处理函数(注意不是函数调用不能带括号),该函数接收一个参数——驱动对象,
# 即WebDriverWait在检查调用该函数时会传入驱动对象;这里我传入了一个lambda表达式来判断首页
# 的标志性按钮是否已加载完成
结语:splinter很强大,还有很多api没有整理,请自行去官网查看,链接在这里
python splinter api整理相关推荐
- Python DataFrame Api整理
DataFrame是提供了很多非常强大的表格管理函数,可以方便的处理表格型数据,DataFrame可以看成每一列都是一个Series组成的表格 DataFrame初始化 import pandas a ...
- python常用api_[原创]IDAPython常用API整理
IDAPython常用API整理:通过一些脚本和<IDA Pro权威指南>,查阅官网的IDAPython API整理,不算很多,有待增添. idaapi.MinEA():获取载入程序的最小 ...
- Python库全部整理出来了,非常全面
库名称简介 Chardet 字符编码探测器,可以自动检测文本.网页.xml的编码. colorama 主要用来给文本添加各种颜色,并且非常简单易用. Prettytable 主要用于在终端或浏览器端构 ...
- [转载] 花了三个月终于把所有的Python库全部整理了!祝你早日拿到高薪!
参考链接: Python | 扩展和自定义django-allauth 库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并 ...
- 花了三个月终于把所有的Python库全部整理了!祝你早日拿到高薪!
库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构建格式 ...
- 花了1个月时间,把Python库全部整理出来了,覆盖所有,建议收藏
目录 库名称简介 文件处理 图像处理 游戏和多媒体 大数据与科学计算 人工智能与机器学习 系统与命令行 数据库 在这里还是要推荐下我自己建的Python学习群:705933274,群里都是学Pytho ...
- 用时三个月,终于把所有的Python库全部整理了!拿去别客气!
库名称简介 进群:548377875 即可获取数十套PDF以及大量的学习教程!都是小编精心整理的,也拿去 别客气! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colora ...
- Python第三方库整理
wheel包下载地址:www.lfd.uci.edu/~gohlke/pythonlibs 数据库: MySql: { 1. MySql-Python:https://pypi.python.org/ ...
- 花了半个月,终于把Python库全部整理出来了,非常全面
转载 原文出处:https://www.jianshu.com/p/d25a9169fe86 库名称简介 Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要 ...
最新文章
- Plant Simulation常用命令
- 计算数值二进制表达式中1的个数
- SolrCloud 分布式集群安装部署(solr4.8.1 + zookeeper +tomcat)
- MySQL ERROR 1045 (28000): Access denied for user 'root'@'192.168.23.224' (using password: YES)
- 《智慧书》格言91-100
- 直播技术总结(二)ijkplayer的编译到Android平台并测试解码库
- MFC字符串操作(三)MFC CString其他用法小结
- Spring源码深度解析
- 视频+案例 | 钟南山院士谈5G医疗
- JAVA案例之使用接口实现手机功能
- 显卡功耗测试用什么软件,显卡功耗测试_AMD显卡_显卡评测-中关村在线
- C语言基础学习——编译过程
- Educational Codeforces Round 101 (Rated for Div. 2)
- 微信公众号授权前端(uniapp为例)
- Java咖啡馆---品味第一杯咖啡
- 【提升coding能力】100道Python练习题11-20
- 车企围攻整车OS,这张“新王牌”怎么打?
- jk触发器改为四进制_四位二进制计数器这样接成十进制计数器
- λ-矩阵(矩阵相似的条件)
- Camtasia视频剪辑功能详解
热门文章
- android 发送彩信监听,第74章、再识Intent-调用发送彩信程序(从零开始学Android)...
- Linux系统编程——进程间通信:命名管道(FIFO)
- ASP.NET C# 用后台代码实现 跨域名访问 返回HTML代码
- Arm和Unity联合推出:适用于移动应用程序的3D美术优化-[3]纹理
- 买了房,还未过户就被法院查封,房屋应该归谁
- docker容器设置开机自启动命令
- win10配置密钥对
- 人人都可直播带货,即构科技携手万商之家打造低门槛直播服务平台
- ContentResolver查询音乐目录
- 时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%