个人博客转至:tybai.com

无聊就想打码,打码使我快乐


Fork me on GitHub

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()

posted on 2016-11-29 10:51  TTyb  阅读(313)  评论(0编辑  收藏  举报

导航


不用多久

我就会升职加薪

当上总经理

出任CEO

迎娶白富美

走上人生巅峰

Pulpit rock