歡迎您光臨本站 註冊首頁

python3+selenium獲取頁面加載的所有靜態資源文件鏈接操作

←手機掃碼閱讀     ljg58026 @ 2020-05-05 , reply:0

這篇文章主要介紹了python3+selenium獲取頁面加載的所有靜態資源文件鏈接操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
軟件版本:
python 3.7.2
selenium 3.141.0
pycharm 2018.3.5
具體實現流程如下,廢話不多說,直接上代碼:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

d = DesiredCapabilities.CHROME
chrome_options = Options()
# 使用無頭瀏覽器 
chrome_options.add_argument('--headless')
chrome_options.add_argument(
    '--user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36')
# 瀏覽器啟動默認最大化 chrome_options.add_argument("--start-maximized"); 
# #該處替換自己的chrome驅動地址 
browser = webdriver.Chrome("D://googleDever//chromedriver.exe", chrome_options=chrome_options,
                           desired_capabilities=d)
browser.set_page_load_timeout(150)
browser.get(
    "https://www.xxx.com")  # 靜態資源鏈接存儲集合 urls = [] #獲取靜態資源有效鏈接 
for log in browser.get_log('performance'):
    if 'message' not in log:
        continue
    log_entry = json.loads(log['message'])
try:
# #該處過濾了data:開頭的base64編碼引用和document頁面鏈接 
 if "data:" not in log_entry['message']['params']['request']['url'] and 'Document' not in
    log_entry['message']['params']['type']: urls.append(
        log_entry['message']['params']['request']['url'])
except Exception as e:
  pass
  print(urls)

打印結果為頁面渲染時加載的靜態資源文件鏈接:
[http://www.xxx.com/aaa.js,http://www.xxx.com/css.css]
以上代碼為selenium獲取頁面加載過程中預加載的各類靜態資源文件鏈接,使用該功能獲取到鏈接後,使用其他插件進行可對資源進行下載!
補充知識:在idea 中python import sys,import requests 報錯
File->Project Structure project -> sdk -> new -> ok
設置編譯參數(主要是設置和檢查Python JDK是否正確)


[ljg58026 ] python3+selenium獲取頁面加載的所有靜態資源文件鏈接操作已經有263次圍觀

http://coctec.com/docs/python/shhow-post-232926.html