python selenium 加载完毕_python+selenium获取页面加载的所有静态资源文件链接操作...

8842d7504af3b163e547e6198534124e.png

这篇文章主要介绍了python3+selenium获取页面加载的所有静态资源文件链接操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

直接上代码:

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://w ww.xxx.com/css.css ]

以上代码为selenium获取页面加载过程中预加载的各类静态资源文件链接,使用该功能获取到链接后,使用其他插件进行可对资源进行下载!

在idea 中python import sys,import requests 报错

File->Project Structure
project -> sdk -> new -> ok

设置编译参数(主要是设置和检查Python JDK是否正确)

2078ba8467db40bdf4ed095cf3eec9ed.png

以上这篇python3+selenium获取页面加载的所有静态资源文件链接操作就是小编分享给大家的全部内容了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值