您可以从脚本标记中获取所有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
该数字是任意的。我猜JS的不到50000个字符实际上可能不是“很多”,因为该页面可能不会每次调用每个函数。这可能会取决于用户的用途。
但是,如果您可以假设精心设计的网站只包含必要的脚本,那么字符数仍然可以作为它运行多少JS的相关指标。
您是否尝试过[机械化](http://wwwsearch.sourceforge.net/mechanize/)这些广泛使用js页面? – alecxe 2013-05-13 21:32:41