使用selenium爬取動態頁面

在爬取某些頁面時,由於其一些內容是使用js動態加載,導致只能爬取到部分的內容(靜態內容)。此時,可以使用selenium庫來解決。

# coding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 爲了將Chrome不彈出界面,實現無界面爬取
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options)

# 發送請求,這裏選取的某當網一頁面。
driver.get("http://product.dangdang.com/23997502.html#ddclick_reco_book")

# 保存爬取的網頁內容
with open("111.html", "w", encoding="utf-8") as f:
    f.write(driver.page_source)

# 退出瀏覽器
driver.quit()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章