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

作者:举个栗子不容易 时间:2022-11-02 19:18:36 

软件版本:

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是否正确)

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

来源:https://blog.csdn.net/qq_32201423/article/details/91419523

标签:python3,selenium,静态文件,链接
0
投稿

猜你喜欢

  • Mybatis实现分包定义数据库的原理与过程

    2024-01-23 12:33:55
  • 关于Go 空结构体的 3 种使用场景

    2024-05-22 17:45:21
  • python 匹配url中是否存在IP地址的方法

    2023-04-13 14:29:08
  • Python特效之文字成像方法详解

    2021-08-09 09:34:06
  • Python实现的手机号归属地相关信息查询功能示例

    2021-02-05 23:10:05
  • Python实现多条件筛选目标数据功能【测试可用】

    2023-07-17 21:03:46
  • 适合Python初学者的一些编程技巧

    2022-01-14 11:19:09
  • Win 10下Anaconda虚拟环境的教程

    2022-09-18 16:42:06
  • 用好FrontPage2003的九大功能

    2008-02-21 14:29:00
  • sql语句之数据操作介绍

    2024-01-22 18:09:39
  • python 实现网易邮箱邮件阅读和删除的辅助小脚本

    2022-03-17 10:55:34
  • python+tkinter编写电脑桌面放大镜程序实例代码

    2023-08-02 17:10:43
  • 获取 Textarea 的光标位置

    2010-11-30 21:33:00
  • python 将列表里的字典元素合并为一个字典实例

    2021-07-26 15:45:09
  • CentOS 6.5中安装Python 3.6.2的方法步骤

    2022-10-11 09:13:43
  • Django代码性能优化与Pycharm Profile使用详解

    2022-06-28 21:13:06
  • PHP入门基础之注释的写法

    2023-10-20 07:26:17
  • 如何在python中使用selenium的示例

    2023-07-15 20:52:09
  • 利用python实现简单的循环购物车功能示例代码

    2021-05-12 14:52:58
  • Centos6.5在线安装mysql 8.0详细教程

    2024-01-15 01:02:57
  • asp之家 网络编程 m.aspxhome.com