前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3+selenium获取页面加载的所有静态资源文件链接操作

python3+selenium获取页面加载的所有静态资源文件链接操作

作者头像
砸漏
发布2020-11-02 14:44:04
2.5K0
发布2020-11-02 14:44:04
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

软件版本:

python 3.7.2

selenium 3.141.0

pycharm 2018.3.5

具体实现流程如下,废话不多说,直接上代码:

代码语言:javascript
复制
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是否正确)

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档