splinter安装与入门
安装
splinter安装
pip安装
pip install splinter
以上安装会同时安装selenium。
源码安装
git clone git://github.com/cobrateam/splinter.git
cd splinter
python setup.py install
Splinter的使用必修依靠Cython、lxml、selenium这三个软件.selenium在安装splinter会自动被安装。
pip install Cython
pip install lxml
安装测试
安装完成后,可以直接在python的交互环境中验证是否安装成功。
>>> from splinter.browser import Browser
>>> browser = Browser()#默认使用Firefox,如果未安装会报错
>>> b = Browser("chrome")#指定浏览器,使用chrome浏览器
如果没有错误会打开Chrome浏览器。
下面的代码,会让上面打开的浏览器里打开百度首页。
>>> b.visit("http://www.baidu.com/")
异常
执行可能会出如下错误:
1.geckodriver’ executable needs to be in PATH.
Traceback (most recent call last):File "C:\Users\xuhuan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 74, in startstdout=self.log_file, stderr=self.log_file)File "C:\Users\xuhuan\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__restore_signals, start_new_session)File "C:\Users\xuhuan\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_childstartupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
这是缺少geckodriver,到https://github.com/mozilla/geckodriver/releases下载指定的版本,然后将解压的位置放入系统的环境变量中。
2.未安装Firefox浏览器
报错原因,splinter默认使用浏览器firefix。
如果要splinter支持Chrome浏览器,需安装chromedriver。到https://chromedriver.storage.googleapis.com/index.html下载指定版本的chromedriver,同geckodriver一样,将解压的位置放入系统的环境变量中。
使用
利用百度搜索
百度的首页应该都见过,关键就是一个输入框,一个百度一下的按钮。搜索发起就是在文本框输入想输入的内容,点击百度一下的按钮即可。目前百度搜索可以只在文本框输入内容就会自动发起搜索。但是如果多次搜索操作,还是需要点击百度一下的按钮的。
所以要实现百度自动搜索只需要两步:
- 将要搜索的内容填充到搜索框
- 点击百度一下按钮
先来分析下百度首页,如下图:
由上可以发现,百度搜索框input的id=kw,name=wd。百度一下按钮的id=su。
下面就是splinter如何操作浏览器了。
splinter操作浏览器使用splinter的Browser。通过API发现可以我们想要的填充数据和点击方法。
填充数据
方法 | 描述 |
---|---|
fill(name, value) | 通过控件name赋值 |
fill_form(field_values) | 通过控件name赋值,参数是字典类型,即key为控件的name,字典项为要赋的值。支持text, password, textarea, checkbox, radio and select.checkbox必须制定字段字典项为boolean 类型 |
通过api可以看出,使用fill或fill_form填充数据似乎只能通过name填充。
选中页面某个控件
方法 | 描述 |
---|---|
find_by_css(css_selector) | 按css选择器查找页面元素 |
find_by_id(id) | 按id查找页面元素 |
find_by_name(name) | 按name查找页面元素 |
find_by_tag(tag) | 按tag查找页面元素 |
find_by_text(text) | 按text查找页面元素 |
find_by_xpath(xpath) | 使用xpath选择器查询当前页面内容 |
find_link_by_href(href) | 通过href查找当前页面中link |
find_link_by_partial_href(partial_href) | 通过部分href值匹配link |
find_link_by_partial_text(partial_text) | 通过部分text值匹配link |
find_link_by_text(text) | 通过text查找link |
find_option_by_text(text) | 通过text查找option元素 |
find_option_by_value(value) | 通过value查找option元素 |
可以看出,splinter提供了丰富的api来进行页面的匹配。
通过上面的介绍,应该可以取得我们想要的元素了,下面就是对取得元素的操作。
这里只先说点击的操作,其他的可以参见splinter元素操作api.
方法 | 描述 |
---|---|
click() | 单击该元素 |
通过上面的介绍,自动百度搜索的代码就呼之欲出了。
# -*- coding: utf-8 -*-
from splinter.browser import Browserb = Browser("chrome")
b.visit("http://www.baidu.com")
dict={"wd":"splinter"}
#b.fill("wd","splinter")
b.fill_form(dict)
button = b.find_by_value(u"百度一下")
button.click()
当然上面的实现只是一种方式,通过api列表就可以发现好多不同的实现方式。比如,查看splinter元素操作api发现一个fill(value)的方法,也可以对选中的元素赋值,而不需要使用Browser对象。
text = b.find_by_id("kw")
text.fill("splinter")
这样同样可以实现
dict={"wd":"splinter"}
或
b.fill("wd","splinter")
splinter安装与入门相关推荐
- Splinter入门(一)splinter安装与使用
splinter介绍 Splinter是一个使用Python测试Web应用程序的开源工具,可以自动化浏览器操作,例如访问URL和与它们的项进行交互.例如,我们使用百度引擎搜索内容,需要再搜索框内输 ...
- Python:Scrapy的安装和入门案例
Scrapy的安装介绍 Scrapy框架官方网址:http://doc.scrapy.org/en/latest Scrapy中文维护站点:http://scrapy-chs.readthedocs. ...
- windows下nodejs express安装及入门网站,视频资料,开源项目介绍
windows下nodejs express安装及入门网站,视频资料,开源项目介绍,pm2,supervisor,npm,Pomelo,Grunt安装使用注意事项等总结 第一步:下载安装文件 下载地址 ...
- TensorFlow 安装及入门
安装 入门 TensorFlow简介 基本操作 loading lazy 安装 上TensorFlow官网按照教程安装. 入门 以下学习内容follow这篇博客 TensorFlow简介 Tensor ...
- Ubuntu 18.04 安装 redis入门使用
Ubuntu 18.04 安装 redis入门使用 1.安装 sudo apt-get install redis-server 2.查看redis运行状态 ps -ef | grep redis 3 ...
- IDE之EcliPse:EcliPse软件的简介、安装、入门、使用方法之详细攻略
IDE之EcliPse:EcliPse软件的简介.安装.入门.使用方法之详细攻略 目录 EcliPse软件的简介 Eclipse特点 EcliPse软件的安装 EcliPse软件的入门 1.快捷键集合
- Python:Python多种集成开发环境(IDE,编译器)的简介、安装、入门、使用方法之详细攻略
Python:Python多种集成开发环境(IDE,编译器)的简介.安装.入门.使用方法之详细攻略 目录 Python多种编译器的简介.安装.入门.使用方法 1.Anaconda 2.ipython ...
- Postman:Postman简介、安装、入门使用方法详细攻略
Postman:Postman简介.安装.入门使用方法详细攻略 目录 Postman简介 Postman安装 Postman入门 1.图文教程 1.1.get案例 1.2.post案例 1.3.get ...
- Ansible 安装和入门
Ansible 安装和入门 Ansible安装 ansible的安装方法有多种 EPEL源的rpm包安装: [root@ansible ~]#yum install ansible 编译安装 yum ...
最新文章
- pycharm连接远程服务器并进行代码上传+远程调试
- 最新Java面试题2021年,常见面试题及答案汇总
- CentOS6.4安装nginx1.4
- JS的trim()方法
- Playing Video on iPhone Cocos2D-X
- 到外国的网站写英语留言
- 字符串MD5加密和产生一个随机密码的方法
- windows 文件名太长无法删除的解决方法
- open source的最大好处是什么?
- 《leetcode》longest-consecutive-sequence
- Yii的路由机制分析
- dw替换多个html标签,DW查找替换的技巧
- emmc固件开发_UP2开发板简易开箱(二)
- 中国移动5G商用首批开放50城 明年扩展至全国地级以上城市
- spearman相关系数(斯皮尔曼相关系数是什么意思)
- python查看微信撤回消息_Python查看微信好友撤回的消息
- ArcGIS Zonal Statistics as Table 工具报错解决
- Java利用PdfBox实现Pdf转图片
- 今年还能回家过年吗?查询下各地出行隔离政策
- win2003终端服务器超出了最大允许连接数解决方案