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

posted @ 2017-08-25 20:33  jhc888007  阅读(443)  评论(0编辑  收藏  举报