python3_phantomJS_test
phantomJS和selenium差不多,几乎不相上下,使用会麻烦一点,但是比selenium快很多:
# !/usr/bin/python3.4
# -*- coding: utf-8 -*-
from selenium import webdriver
# 下载phantomjs:http://phantomjs.org/download.html
# driver = webdriver.PhantomJS()
# driver.get("https://www.baidu.com/")
# data = driver.title
# print(data)
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# 建立一个字典
dcap = dict(DesiredCapabilities.PHANTOMJS)
# 5秒超时
dcap["phantomjs.page.settings.resourceTimeout"] = 5000
# 无图模式
dcap["phantomjs.page.settings.loadImages"] = False
# 头部
dcap[
"phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5"
dcap["phantomjs.page.settings.referer"] = "https://www.baidu.com/"
# 将设置加载到浏览器
browser = webdriver.PhantomJS(executable_path='C:/Python34/Scripts/phantomjs', desired_capabilities=dcap)
# 打开网址
browser.get("https://www.baidu.com/")
# 查看phantom的详细参数
cap_dict = browser.desired_capabilities
for key in cap_dict:
print('%s: %s' % (key, cap_dict[key]))
# 打印网址
print(browser.current_url)
# 加载后的页面
html = browser.page_source
# 这里可以用BS4或者xpath解析
# phantom自带的xpath和selenium一样:browser.find_element_by_xpath('//ul[@class="products"]/a')
print(html)
# 关闭虚假浏览器
browser.quit()
python3_phantomJS_test的更多相关文章
随机推荐
- 安卓设备通过USB接口读取UVC摄像头权限问题
libusb for Android================== Building:--------- To build libusb for Android do the following ...
- 转帖-[教程] Win7精简教程(简易中度)2016年8月-0day
[教程] Win7精简教程(简易中度)2016年8月 0day 发表于 2016-8-19 16:08:41 https://www.itsk.com/thread-370260-1-1.html ...
- 20145224&20145238 《信息安全系统设计基础》第二次实验
20145224&20145238 <信息安全系统设计基础>第二次实验 课程:信息安全系统设计基础 班级:1452 姓名:陈颢文 荆玉茗 学号:20145224 20145238 ...
- git删除远程仓库的某次错误提交
改日写git的各种本地恢复 今天不小心把一个分支合并到master上了,上网查了一下回复的方法,简单来说就是: 在本地 把远程的master分支删除再把reset后的分支内容给push上去 新建old ...
- 坑爹的属性,android:descendantFocusability用法简析
开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承 BaseAdapter,在adapter中按照需求进行编写,问题就出现 ...
- Java小bug
1.使用split函数,将字符串按"."分开,写法如下: String str = "a.b.c"; String[] items = str.split(&q ...
- Citrix 虚拟化笔记
利用win7 x64/Vmware workstation 10练习Citrix虚拟化 [安装Xenserver 6.2] 1)硬盘空间不足:要求最小8G 2)不支持硬件虚拟化:找到建立的XENSER ...
- [Jquery]判断数据类型
typeof [1, 2, 4] === 'object';typeof new Date() === 'object'; typeof null === 'object'; 由于typeof数组. ...
- SSH(2)
1.用户登录 index页面跳转到登录页面 <% String path = request.getContextPath(); String basePath = request.getSch ...
- JBoss集群中启用HTTPS协议
Generate server certificate Note: If you already have certificate created then this section can be i ...