当您使用浏览器浏览网页时,下载页面代码后,浏览器会下载所有资源(CSS、JS 和图像)。
有没有办法列出页面资产(内部和外部资产)的所有 URL?
这个想法是监控外部和内部资产的变化。
答案1
我写了一个Python脚本,它可能会做你想做的事:
#!/usr/bin/env python2
# -*- coding: ascii -*-
"""list_assets.py"""
from bs4 import BeautifulSoup
import urllib
import sys
import jsbeautifier
import copy
# Define a function to format script and style elements
def formatted_element(element):
# Copy the element
formatted_element = copy.copy(element)
# Get beautified element text
formatted_text = jsbeautifier.beautify(formatted_element.text)
# Indent all of the text
formatted_text = "\n".join([" " + line for line in formatted_text.splitlines()])
# Update the script body
formatted_element.string = "\n" + formatted_text + "\n "
# Return the beautified element
return(formatted_element)
# Load HTML from a web page
html = urllib.urlopen(sys.argv[1]).read()
# Parse the HTML
soup = BeautifulSoup(html, "html.parser")
# Extract the list of external image URLs
image_urls = [image['src'] for image in soup.findAll('img') if image.has_attr('src')]
# Extract the list of external CSS URLs
css_urls = [link['href'] for link in soup.findAll('link') if link.has_attr('href')]
# Extract the list of external JavaScript URLs
script_urls = [script['src'] for script in soup.findAll('script') if script.has_attr('src')]
# Extract the list of internal CSS elements
styles = [formatted_element(style) for style in soup.findAll('style')]
# Extract the list of internal scripts
scripts = [formatted_element(script) for script in soup.findAll('script') if not script.has_attr('src')]
# Print the results
print("Images:\n")
for image_url in image_urls:
print(" %s\n" % image_url)
print("")
print("External Style-Sheets:\n")
for css_url in css_urls:
print(" %s\n" % css_url)
print("")
print("External Scripts:\n")
for script_url in script_urls:
print(" %s\n" % script_url)
print("")
print("Internal Style-Sheets:\n")
for style in styles:
print(" %s\n" % style)
print("")
print("Internal Scripts:\n")
for script in scripts:
print(" %s\n" % script)
这些是我使用的(值得注意的)软件包:
该脚本打印外部资源的 URL 和内部资源的元素标签本身(经过美化/美化)。我就如何格式化结果做出了一些即兴的风格选择,这些结果可以很容易地修改或改进。
要查看其实际效果,请参阅以下 HTML 文件示例 ( assets.html
):
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>assets.html</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
<script src="myscripts.js"></script>
</head>
<body>
<script>alert( 'Hello, world!' );</script>
<img src="https://www.python.org/static/community_logos/python-logo.png">
<p>I'm the content</p>
</body>
</html>
以下是我们执行脚本的方式(在本地文件上):
python list_assets.py assets.html
这是输出:
Images:
https://www.python.org/static/community_logos/python-logo.png
External Style-Sheets:
mystyle.css
External Scripts:
myscripts.js
Internal Style-Sheets:
<style>
body {
background - color: linen;
}
h1 {
color: maroon;
margin - left: 40 px;
}
</style>
Internal Scripts:
<script>
alert('Hello, world!');
</script>
最后,这里有一些我用作参考的帖子,您可能会发现它们有用: