纵有疾风起
人生不言弃

Python爬虫_Selenium与PhantomJS入门

Selenium 是一个Web的自动化测试工具,最初是为网站自动化测试而开发的。类型像我们玩游戏用的按键精灵,可以按指定的命令自动化操作,不同是Selenium可以直接运行在浏览器上,它支持所有主流的浏览器(包括PhantomJS这些无界面的浏览器)。

Selenium可以根据我们的指令,让浏览器自动加载页面,获取需要的页面,甚至页面截屏,或者判断网站某些动作是否发生。

Selenium自己不带浏览器,不支持浏览器的功能。它需要与第三方浏览器结合在一起才能使用。但是我们有时候需要让它内嵌在代码中运行,所以我们使用一个加PhantomJS的工具代替真实的浏览器。

可以从PyPI网站下载Selenium库pypi.python.org/simple/sule…,也可以用第三方管理器pip命令安装:pip install selenium
Selenium官方参考文档:selenium-python.readthedocs.io/index.html

PhantomJS

PhantomJS 是一个基于Webkit的“无界面”(headless)L浏览器,他会把网站加载到内存并执行页面上的JavaScript,因为不会展示图形界面,所以运行起来比完整的浏览器更高效。

如果我们把Selenium和PhantomJS结合在一起,就可以运行一个非常强大的网络怕丑,这个爬虫可以处理JavaScript、Cookie、headers,以及任何我们真实用户需要做的事情。

Python爬虫_Selenium与PhantomJS入门插图
file

快速入门

Selenium库里有一个叫WebDriver的API。WebDriver 可以控制浏览器的操作,它可以想BeautifulSoup或者其他的Selector对象一样用来查找页面的元素,与页面上的元素进行交互(发送文本、点击等),以及执行其他动作来运行网络爬虫。

Python爬虫_Selenium与PhantomJS入门插图1
file

Python爬虫_Selenium与PhantomJS入门插图2
file

页面操作

Selenium的WebDriver提供了各种方法来寻找元素,假设下面有一个表单输入框:

Python爬虫_Selenium与PhantomJS入门插图3
file
Python爬虫_Selenium与PhantomJS入门插图4
file

定位UI元素(WebElements)

关于元素的选取,有如下的API单个元素选取

Python爬虫_Selenium与PhantomJS入门插图5
file

1.By ID

Python爬虫_Selenium与PhantomJS入门插图6
file

实现

Python爬虫_Selenium与PhantomJS入门插图7
file

———or——-

Python爬虫_Selenium与PhantomJS入门插图8
file

2.By Class Name

Python爬虫_Selenium与PhantomJS入门插图9
file

实现

Python爬虫_Selenium与PhantomJS入门插图10
file

—————-or——————–

Python爬虫_Selenium与PhantomJS入门插图11
file

3.By Tag Name

Python爬虫_Selenium与PhantomJS入门插图12
file

实现

Python爬虫_Selenium与PhantomJS入门插图13
file

4.By Name

Python爬虫_Selenium与PhantomJS入门插图14
file

实现

Python爬虫_Selenium与PhantomJS入门插图15
file

5.By Link Text

Python爬虫_Selenium与PhantomJS入门插图16
file

实现

Python爬虫_Selenium与PhantomJS入门插图17
file

6.By Partial Link Text

Python爬虫_Selenium与PhantomJS入门插图18
file

实现

Python爬虫_Selenium与PhantomJS入门插图19
file

7.By CSS

Python爬虫_Selenium与PhantomJS入门插图20
file
Python爬虫_Selenium与PhantomJS入门插图21
file

8.By XPath

Python爬虫_Selenium与PhantomJS入门插图22
file

实现

Python爬虫_Selenium与PhantomJS入门插图23
file

鼠标动作链

有些时候,我们需要再页面上模拟一些鼠标操作,比如双击、右击、拖拽甚至按住不同等,我们可以通过导入ActionChains类来做到。

示例

Python爬虫_Selenium与PhantomJS入门插图24
file

填充表单

我们已经知道怎样想文本框中输入文字,但是有时候我们会碰到 标签的下拉框。直接点击下拉框中的选项不一定可以行。

Python爬虫_Selenium与PhantomJS入门插图25
file

Selenium专门提供了Select类处理下拉框。其实WebDriver中提供了一个叫Select的方法,可以帮助我们完成这些事情:

Python爬虫_Selenium与PhantomJS入门插图26
file

以上是三种选择下拉框的方式,它可以根据索引来选择,可以根据值来选择,可以根据文字来选择。注意:

Python爬虫_Selenium与PhantomJS入门插图27
file

全部取消怎么办?

Python爬虫_Selenium与PhantomJS入门插图28
file

弹窗处理

当你触发了某个时间之后,页面出现了弹窗提示,处理这个提示或者获取提示信息方法如下:

Python爬虫_Selenium与PhantomJS入门插图29
file

页面切换

一个浏览器肯定会有很多窗口,所以我们肯定要有方法来实现窗口的切换,切换窗口的方法如下:

Python爬虫_Selenium与PhantomJS入门插图30
file

也可以使用window_handles方法来获取每个窗口的操作对象。例如:

Python爬虫_Selenium与PhantomJS入门插图31
file

页面的前进和后退

操作页面的前进和后退功能:

Python爬虫_Selenium与PhantomJS入门插图32
file

Cookies

获取页面每个Cookies值,用法如下:

Python爬虫_Selenium与PhantomJS入门插图33
file

删除Cookies,用法如下:

Python爬虫_Selenium与PhantomJS入门插图34
file

页面等待

注意:这是非常重要的一部分!

Python爬虫_Selenium与PhantomJS入门插图35
file

显式等待

显示等待指定了某个条件,然后设置最长等待事件。如果在这个时间还找到没有元素,那么便会抛出异常。

Python爬虫_Selenium与PhantomJS入门插图36
file

如果不写参数,程序默认会0.5s调用一次来来查看安苏是否已经生成,如果本来元素时存在的,那么会立即返回。

下面是一些内置的等待条件,你可以直接调用这些条件,而不用自己写某些等待条件了。

Python爬虫_Selenium与PhantomJS入门插图37
file

隐式等待

隐式等待比较简单,就是简单地设置一个等待时间,单位为秒。

Python爬虫_Selenium与PhantomJS入门插图38
file

如果不设置,默认等待时间为0。

Python爬虫_Selenium与PhantomJS入门插图39
file

点击了解更多获取PythonWeb开发,数据分析,爬虫等学习资料,

了解更多

文章转载于:https://www.jianshu.com/p/0c67512a0129

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » Python爬虫_Selenium与PhantomJS入门
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录