python3_phantomJS_test

2019-07-02 09:36:14 浏览数 (1)

phantomJS和selenium差不多,几乎不相上下,使用会麻烦一点,但是比selenium快很多:

代码语言:javascript复制
# !/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()

0 人点赞