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


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