python3+selenium獲取頁面加載的所有靜態(tài)資源文件鏈接操作
軟件版本:
python 3.7.2
selenium 3.141.0
pycharm 2018.3.5
具體實現(xiàn)流程如下,廢話不多說,直接上代碼:
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驅(qū)動地址 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") #靜態(tài)資源鏈接存儲集合 urls = [] #獲取靜態(tài)資源有效鏈接 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)
打印結(jié)果為頁面渲染時加載的靜態(tài)資源文件鏈接:
[http://www.xxx.com/aaa.js,http://www.xxx.com/css.css]
以上代碼為selenium獲取頁面加載過程中預(yù)加載的各類靜態(tài)資源文件鏈接,使用該功能獲取到鏈接后,使用其他插件進行可對資源進行下載!
補充知識:在idea 中python import sys,import requests 報錯
File->Project Structure
project -> sdk -> new -> ok
設(shè)置編譯參數(shù)(主要是設(shè)置和檢查Python JDK是否正確)
以上這篇python3+selenium獲取頁面加載的所有靜態(tài)資源文件鏈接操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
數(shù)據(jù)挖掘之Apriori算法詳解和Python實現(xiàn)代碼分享
這篇文章主要介紹了數(shù)據(jù)挖掘之Apriori算法詳解和Python實現(xiàn)代碼分享,本文先是對Apriori算法做了詳細介紹,然后給出了Python版實現(xiàn)代碼,需要的朋友可以參考下2014-11-11python中的split、rsplit、splitlines用法說明
這篇文章主要介紹了python中的split、rsplit、splitlines用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Django shell調(diào)試models輸出的SQL語句方法
今天小編就為大家分享一篇Django shell調(diào)試models輸出的SQL語句方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08基于Flask+websocket實現(xiàn)一個在線聊天室
在今天的互聯(lián)網(wǎng)時代,實時通信成為了許多應(yīng)用和服務(wù)的核心特色,在本文中,我們將介紹如何使用 Flask 和 Websockets 通過 Flask-SocketIO 框架創(chuàng)建一個簡單的在線聊天室,感興趣的可以跟隨小編一起了解下2023-09-09