天天看點

python爬蟲——使用selenium+chrome options爬取站長素材頁面源碼

一.站長素材

1.需要爬取的内容

python爬蟲——使用selenium+chrome options爬取站長素材頁面源碼
python爬蟲——使用selenium+chrome options爬取站長素材頁面源碼

2.代碼

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
# webdriver 路徑
path = r'E:\chromedriver_win32\chromedriver.exe'
# 建立無界面浏覽器
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(executable_path=path, options=chrome_options)

# 站長素材高清圖檔-科技圖檔url
url = 'http://sc.chinaz.com/tupian/kejitupian.html'
browser.get(url)
time.sleep(3)
# 第一次儲存html代碼
with open('kejitupian1.html', 'w', encoding='utf8') as fp:
    fp.write(browser.page_source)
# 滾動,執行js
js = 'window.scrollTo(0,document.body.scrollHeight)'
browser.execute_script(js)
time.sleep(3)
# 第二次儲存html代碼
with open('kejitupian2.html', 'w', encoding='utf8') as fp:
    fp.write(browser.page_source)

# 關閉浏覽器
browser.quit()
           

3.結果對比

第一次抓取:

python爬蟲——使用selenium+chrome options爬取站長素材頁面源碼

第二次抓取:

python爬蟲——使用selenium+chrome options爬取站長素材頁面源碼

繼續閱讀