PhantomJS介绍与使用

一、什么是PhantomJS?

  一款无界面浏览器。

二、python如何爬取动态加载页面?

  selenium+PhantomJS(爬虫终极解决方案)

三、selenium操作PhantomJS

1、下载PhantomJS

https://phantomjs.org/download.html

2、代码示例

#!/usr/local/bin/python3.7

from selenium import webdriver
import time

# phantomJS路径
path = '/Users/mozili/Documents/PhantomJS/phantomjs-2.1.1-macosx/bin/phantomjs'
# 创建浏览器对象
browser = webdriver.PhantomJS(path)

# 打开百度并操作
url = 'https://www.baidu.com'
browser.get(url)
time.sleep(1)
# 截图
browser.save_screenshot('Reptile/phantomjs_img/baidu.png')
# 定位搜索框
search = browser.find_element_by_id('kw')
time.sleep(1)
# 在搜索框输入内容
search.send_keys('美女')
time.sleep(1)
# 截图
browser.save_screenshot('Reptile/phantomjs_img/meinv.png')
# 关闭浏览器
browser.quit()
原文地址:https://www.cnblogs.com/lxmtx/p/13019902.html