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