Windows下 Python Selenium PhantomJS 抓取网页并截图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

  • 进入Python27\\Scripts
  • 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:\\Python27\\phantomjs.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

以上是关于Windows下 Python Selenium PhantomJS 抓取网页并截图的主要内容,如果未能解决你的问题,请参考以下文章

windows下python环境+selenium环境搭建

windows下安装配置python + selenium 来驱动firefox

Windows下 Python Selenium PhantomJS 抓取网页并截图

[Python爬虫] 在Windows下安装PIP+Phantomjs+Selenium

Windows下怎么搭建Python+Selenium的自动化环境

windows10下python中selenium的chrome驱动安装与使用