1. 程式人生 > >使用selenium和phantomJS瀏覽器獲取網頁內容的小演示

使用selenium和phantomJS瀏覽器獲取網頁內容的小演示

# 使用selenium和phantomJS瀏覽器獲取網頁內容的小演示

# 匯入包
from selenium import webdriver


# 使用selenium庫裡的webdriver方法呼叫PhantomJS瀏覽器例項一個瀏覽器的操作物件
# 括號裡的引數為PhantomJS瀏覽器在電腦裡的絕對路徑,這裡已經添加了環境變數,所以可以省略
web = webdriver.PhantomJS()
#web = webdriver.PhantomJS(executable_path="./phantomjs")

# 瀏覽器獲取一個url
web.get("https://fanyi.baidu.com/
") # 儲存瀏覽器截圖 web.save_screenshot("baidu.png") # 瀏覽器尋找一個id為括號裡的名字的標籤, 然後輸入 hello world web.find_element_by_id("baidu_translate_input").send_keys("hello world") # 儲存截圖 web.save_screenshot("baidu1.png") # 瀏覽器尋找一個id為括號裡的名字的標籤, 然後點選(模擬滑鼠左鍵) web.find_element_by_class_name("select-inner
").click() # 儲存截圖 web.save_screenshot("baidu2.png")