• 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()
    
    
  • 相关阅读:
    选项卡
    使用script创建标签添加属性值和添加样式
    判断表单不能为空(表格形式的)
    数组
    关于meta元信息元素
    js判断数据类型
    Ising模型的Metropolis模拟中的组态阻塞和振荡
    python 画能级图
    python 调用 shell 命令,制作用户界面
    打包 python 程序,变成一个可执行文件
  • 原文地址:https://www.cnblogs.com/TTyb/p/6112663.html
Copyright © 2020-2023  润新知