您可以从脚本标签中获取所有 JavaScript,将它们全部加起来,并检查长度是否不超过您认为构成“大量”JavaScript 的任何数量。
# get all script tags
scripts = browser.find_elements_by_tag_name("script")
# create a string to add all the JS content to
javaScriptChars = "";
# create an list to store urls for external scripts
urls = list()
# for each script on the page...
for script in scripts
# get the src
url = script.get_attribute("scr")
# if script is external (has a 'src' attribute)...
if url.__len__() > 0:
# add the url to the list (will access it later)
urls.append(url)
else:
# the script is inline - so just get the text inside
javaScriptChars = javaScriptChars + script.getAttribute("textContent");
# for each external url found above...
for url in urls
# open the script
driver.get(url)
# add the content to our string
javaScriptChars = javaScriptChars + driver.page_source
# check if the string is longer than some threshold you choose
if javaScriptChars.__len__() > 50000:
# JS contains more than 5000 characters
数字是任意的。我猜不到 50000 个字符的 JS 实际上可能不是“很多”,因为页面可能不会每次都调用每个函数。这可能在一定程度上取决于用户的行为。
但是,如果您可以假设一个设计良好的网站只包含必要的脚本,那么字符数仍然可能是它运行多少 JS 的相关指标。