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()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM