我正在寻找一种方法来确定网站上使用的所有Javascript的名称。简单地使用请求库下载网站的源代码是不合适的,因为这不会产生所有使用的Javascript。
例如网站
https://www.grantthornton.global/en/
使用Google Analytics(Analytics.js)可以看到,使用chrome的“网络”选项卡可以查看所有使用的javascripts。
但是,您无法确定分析的使用情况。js通过sourcode单独作为分析。js是通过谷歌标签管理器加载的。
我目前的方法是使用selenium加载网站,并通过browsermob代理记录所有数据。然后,我可以通过检查URL来检查已访问的所有Javascript(例如:
https://www.google-analytics.com/analytics.js
)
还有比这更好的方法吗:
from selenium import webdriver
from browsermobproxy import Server
import pprint, time
server = Server("browsermob-proxy-2.1.4\\bin\\browsermob-proxy")
server.start()
proxy = server.create_proxy({'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True})
service_args = ["--proxy=%s" % proxy.proxy, '--ignore-ssl-errors=yes']
driver = webdriver.PhantomJS("phantomjs-2.1.1-windows\\bin\\phantomjs", service_args=service_args)
proxy.new_har()
driver.get('URL GOES HERE')
time.sleep(3)
all_requests = [entry['request']['url'] for entry in proxy.har['log']['entries']]
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(proxy.har)
基于Florent B方法的解决方案。webdriver已被需要下载的chrome webdriver取代,而不是phantomjs:
from selenium import webdriver
import pprint, time
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://www.URLGOESHERE.com")
time.sleep(3)
scripts = driver.execute_script("""return window.performance.getEntriesByType("resource").filter(e => e.initiatorType === 'script').map(e => e.name.match(/.+\/([^?]+)/)[1]);""")
driver.close()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(scripts)