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的更多相关文章
随机推荐
- 转:画图解释 SQL join 语句
画图解释 SQL join 语句 我认为 Ligaya Turmelle 的关于SQL联合(join)语句的帖子对于新手开发者来说是份很好的材料.SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看 ...
- 【更新】【封装必备】封装辅助 - 清理&优化工具 For Win7(IT天空会员专版)
https://www.itsk.com/thread-353560-1-4.html nqawen 发表于 2015-7-9 17:26:37 本帖最后由 Amz 于 2015-11-25 10 ...
- linux笔记:用户和用户组管理-用户配置文件
用户信息文件(/etc/passwd): 影子文件(/etc/shadow) 组信息文件(/etc/group)和组密码文件(/etc/gshadow):
- NGUI Camera's raycast hit through the UI Layer issue
Raycast into GUI?http://forum.unity3d.com/threads/raycast-into-gui.263397/ << ; Ray myray = UI ...
- 带事物处理的DBHelp和sql语句
DBHelp语句 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- redhat网络基础配置
添加浮动IP: ifconfig eth0:1 192.168.1.106 IP配置文件: BOOTPROTO: 采用的启动协议,有三种选择: (1) none:不使用启动协议 (2) static: ...
- Objective-C( Foundation框架 一 数组(NSArray))
OC数组 NSArray是静态的数组,就是它所指向的内容是不可改变的,它指向一段内存区域,一旦初始化,不能通过它对该内存区域的数据进行修改操作,但是它可以读数据. NSMutableArray是动态的 ...
- WCF初探-23:WCF中使用Message类(下)
前言 在上一篇WCF中使用Message类(上)中,文章介绍了WCF中使用Message类的基本知识和怎样创建消息,本文是承接上一篇文章,如果想要更好的阅读本文,请先阅读上一篇文章.在这篇文章中,我将 ...
- jquery统计页面的pv/ip及停留时间等
我们在做网站的时候经常需要统计网站的访问信息,这里介绍一个用jquery写的一个统计方法 新建一个js文件jun_record.js 代码如下: var start; var end; var tim ...
- input输入框输入文字出现清空文字按钮
$("#J_UserName").keyup(function(){ if($("#J_UserName").val() == "" || ...