简体   繁体   English

如何在JavaScript中加载HTML页面

[英]How to load a html page in javascript

I am trying to load a website into JavaScript so I can scrape it. 我正在尝试将网站加载到JavaScript中,以便进行抓取。 My question is how would i load the website into client side JavaScript? 我的问题是如何将网站加载到客户端JavaScript中? I have tried loading it into a iframe and a JavaScript object element. 我尝试将其加载到iframe和JavaScript对象元素中。

You'd use AJAX. 您将使用AJAX。 In most cases, though, the same-origin policy will trip you up - JavaScript on example.com can't access the source code of example.net because they're different domains. 在大多数情况下,虽然, 同源策略将你绊倒- JavaScript的example.com无法访问的源代码example.net ,因为他们是不同的域。

If both sites are under your control, CORS can fix this . 如果两个站点都在您的控制之下,则CORS可以解决此问题 If you're trying to scrape something like Facebook, you'd have to proxy the requests with some sort of server-side language like PHP. 如果您尝试抓取Facebook之类的内容,则必须使用某种服务器端语言(例如PHP)代理请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM