zoukankan      html  css  js  c++  java
  • Windows下 Python Selenium PhantomJS 抓取网页并截图

    安装Python

    • https://www.python.org/downloads/release
    • 下载安装
    • 将Python目录加入PATH

    安装SetupTools

    • https://pypi.Python.org/pypi/setuptools
    • 下载解压到Python目录并进入
    • cmd执行Python setup.py install

    安装Pip

    • https://pypi.Python.org/pypi/setuptools
    • 下载解压到Python目录并进入
    • cmd执行Python setup.py install

    安装Selenium

    • 进入Python27Scripts
    • cmd执行pip install selenium

    安装Geckodriver

    • https://github.com/mozilla/geckodriver/releases
    • 下载解压exe到Python目录

    截取网页(只能截取一页)

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    import sys 
    reload(sys)
    sys.setdefaultencoding('gb18030')
    driver = webdriver.Firefox()
    driver.get("http://www.baidu.com")
    elem = driver.find_element_by_name("wd")
    elem.send_keys("Eastmount")
    elem.send_keys(Keys.RETURN)
    time.sleep(2)
    driver.save_screenshot('baidu.png')
    driver.close()
    driver.quit()

    安装PhantomJS

    • http://phantomjs.org/
    • 下载安装
    • 将目录加入PATH(如果使用有问题则复制exe到Python目录)

    截取整个网页

    # -*- coding:utf-8 -*-
    from selenium import webdriver
    from urllib import quote
    import sys
    reload(sys)   
    sys.setdefaultencoding('utf-8')  
    driver=webdriver.PhantomJS(executable_path="C:Python27phantomjs.exe")
    url=quote("searchType=song&searchKeyWord=鹿晗 Your Song")
    driver.viewportSize={'width':1280,'height':720}
    driver.maximize_window()
    driver.get("http://www.kugou.com/yy/html/search.html#"+url)    
    data = driver.title  
    driver.save_screenshot('your.png')  
    print data  

     效果如下

    参考文献:http://blog.csdn.net/comela/article/details/44101203

    参考文献:http://blog.csdn.net/eastmount/article/details/47799865

    参考文献:http://blog.csdn.net/eastmount/article/details/47023199

    参考文献:http://blog.csdn.net/jinhe123/article/details/69946234

    参考文献:http://blog.csdn.net/sinat_21302587/article/details/53585527

  • 相关阅读:
    Linux中使用 FTP 命令时出现 “-bash: ftp: command not found”
    Jenkins Build step 'Execute shell' marked build as failure
    centos7ping www.baidu.com没有ping通
    linux maven 安装
    位运算初步
    C++ STL:next_permutation和prev_permutation
    逆波兰表达式
    STL与基本数据结构
    学习记录:指针(未整理)
    Codeforces Round #615 (Div. 3) 补题记录
  • 原文地址:https://www.cnblogs.com/jhc888007/p/7429582.html
Copyright © 2011-2022 走看看