[英]How to load JavaScript enabled response using axios or fetch API in JavaScript?
I am working on a personal project in which I want to read the whole HTML of a JavaScript dependent webpage.我正在做一个个人项目,我想在其中阅读 JavaScript 相关网页的整个 HTML。 For Example if I to load this URL in a JavaScript Enabled web browser, this is what I get:
例如,如果我在 JavaScript 启用 web 浏览器中加载此 URL ,这就是我得到的:
However, if I disable JavaScript in the browser, and load the same URL now, I get this:但是,如果我在浏览器中禁用 JavaScript,并现在加载相同的 URL ,我会得到:
This is pretty normal I know.我知道这很正常。
Now I am trying to load the HTML of the same link in JavaScript code using axios HTTP client, and obviously I am getting the HTML of JavaScript disabled webpage as the HTTP response. Now I am trying to load the HTML of the same link in JavaScript code using axios HTTP client, and obviously I am getting the HTML of JavaScript disabled webpage as the HTTP response.
I want to get the HTML(+JS) source as the response of the same link (in which JavaScript is enabled).我想获取 HTML(+JS) 源作为同一链接的响应(其中启用了 JavaScript)。 I don't know how to mimic a JavaScript enabled Web Browser when working with HTTP clients like axios or fetch API.
I don't know how to mimic a JavaScript enabled Web Browser when working with HTTP clients like axios or fetch API.
If you're trying to do this in the browser, you basically can't unless the site you're loading lets you do so (via CORS or similar).如果您尝试在浏览器中执行此操作,除非您正在加载的站点允许您这样做(通过CORS或类似网站),否则您基本上不能这样做。 You'd have to load it into a window or
iframe
, wait for its JavaScript to run, and then access the resulting DOM.您必须将其加载到 window 或
iframe
中,等待其 JavaScript 运行,然后访问生成的 DOM。 But accessing the DOM of a cross-origin page is disabled by default.但是默认情况下禁用访问跨域页面的 DOM。
The only browser-based way I can think of is to write and install a browser extension , since when a user installs an extension, they can grant greater power to the extension than a web page normally has.我能想到的唯一基于浏览器的方法是编写和安装浏览器扩展,因为当用户安装扩展时,他们可以为扩展授予比 web 页面通常具有的更大的权力。
If you're trying to do this in a non-browser environment, you can use a headless browser like headless Chromium or similar.如果您尝试在非浏览器环境中执行此操作,则可以使用无头浏览器,如无头 Chromium 或类似浏览器。 The browser-based restrictions don't apply.
基于浏览器的限制不适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.