您的位置:首页 > 编程语言 > Python开发

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

2019-06-11 11:44 495 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_32201423/article/details/91419523

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)
#静态资源链接存储集合
urls = []
#获取静态资源有效链接
for log in self.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获取页面加载过程中预加载的各类静态资源文件链接,使用该功能获取到链接后,使用其他插件进行可对资源进行下载!

特别声明:本文为原创作品,转载请注明出处来源https://blog.csdn.net/qq_32201423/article/details/91419523

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: