python phantomjs+ selenium2 抓取动态js网页(版本python2.7+)

    xiaoxiao2026-01-15  7

    1、下载phantomjs linux版本

         解压即可

        单独使用方法:../bin/phantomjs create_img.js    xxx.com(可选) 

       示例,抓取网页截图:

       建立一个pageload.js文件

    编写以下js

    var page = require('webpage').create(); page.open('http://cuiqingcai.com', function (status) { console.log("Status: " + status); if (status === "success") { page.render('example.png'); } phantom.exit(); });

    运行

    phantomjs pageload.js

    会在当前目录下看到截图

     2、pip安装selenium

        pip install selenium

    3、selenium+phantomjs 结合写python文件

    #!/usr/bin/python #encoding:utf-8 from selenium import webdriver #写phantomjs执行文件地址 driver = webdriver.PhantomJS(executable_path='/usr/local/soft_download/python_file/phantomjs/bin/phantomjs') driver.get("http://mofangdata.cn/") #driver.find_element_by_id('search_form_input_homepage').send_keys("Nirvana") #driver.find_element_by_id("search_button_homepage").click() print driver.current_url driver.quit() 注意给python 执行权限 chmod u+x 文件名 运行 ./xx.py

    相关资源:python入门教程(PDF版)
    最新回复(0)