使用 python 抓取网站上所有使用过的 javascript
Scrape all used javascripts on a website using python
我正在寻找一种方法来确定网站上使用的所有 javascript 的名称。使用请求库简单地下载网站的源代码是不合适的,因为这不会产生所有使用的 javascript。
例如,网站 https://www.grantthornton.global/en/ 使用 Google Analytics (analytics.js),可以看到使用 chrome 的 "Network" 选项卡来显示所有使用的 javascript。
但是,您无法仅通过源代码确定 analytics.js 的用法,因为 analytics.js 是通过 google-tag-manager 加载的。
我目前的方法是使用 selenium 加载网站并通过 browsermob-proxy 记录所有数据。然后我可以通过检查 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)
您还可以通过 window.performance
API :
获取所有下载的脚本
scripts = driver.execute_script("""
return window.performance.getEntriesByType("resource")
.filter(e => e.initiatorType === 'script')
.map(e => e.name);
""")
print(scripts)
我正在寻找一种方法来确定网站上使用的所有 javascript 的名称。使用请求库简单地下载网站的源代码是不合适的,因为这不会产生所有使用的 javascript。 例如,网站 https://www.grantthornton.global/en/ 使用 Google Analytics (analytics.js),可以看到使用 chrome 的 "Network" 选项卡来显示所有使用的 javascript。
但是,您无法仅通过源代码确定 analytics.js 的用法,因为 analytics.js 是通过 google-tag-manager 加载的。
我目前的方法是使用 selenium 加载网站并通过 browsermob-proxy 记录所有数据。然后我可以通过检查 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)
您还可以通过 window.performance
API :
scripts = driver.execute_script("""
return window.performance.getEntriesByType("resource")
.filter(e => e.initiatorType === 'script')
.map(e => e.name);
""")
print(scripts)