简体   繁体   English

我可以通过Javascript从另一个网页中获取特定页面的HTML代码吗?

[英]Can I grab specific page HTML code from another webpage through Javascript?

I've read about how there are multiple methods to grabbing source code from another webpage via jQuery or using Cross-Domain Requests. 我已经了解了如何通过jQuery或使用跨域请求从另一网页中获取源代码的多种方法。 What I want to try and do is make it so I grab a div that has different code each time a page is loaded and not the source code as a whole. 我想尝试做的是创建一个div,每次加载一个页面时,它具有不同的代码,而不是整个源代码。 So for example, the greater detail you see when you use 'inspect element' or a tool like firebug to dive deeper into the page code. 因此,举例来说,当您使用“检查元素”或诸如Firebug之类的工具深入了解页面代码时,会看到更多详细信息。

Would I be using one of the same methods? 我会使用相同的方法之一吗?

Yes. 是。

If you control BOTH domains you can add the Access_control_CORS header to allow access of cross domain requests and use a headless browser like phantomJS to grab a cached version of rendered HTML page. 如果您同时控制两个域,则可以添加Access_control_CORS标头以允许跨域请求的访问,并使用诸如phantomJS之类的无头浏览器来获取呈现的HTML页面的缓存版本。

If you don't control both domains you will have to write a server side proxy to grab the page and all its resources (you will have to parse the page to get or rewrite links to images, javascripts, stylesheets etc...) then run it through phantomJS to create a HTML snapshot.` 如果您不能同时控制两个域,则必须编写服务器端代理来获取页面及其所有资源(必须解析页面以获取或重写指向图像,javascript,样式表等的链接),然后通过phantomJS运行它以创建HTML快照。

source: 资源:

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
http://phantomjs.org/

NOTE: despite my best efforts, stack overflow is absolutely convinced these links are code. 注意:尽管我已尽力而为,但堆栈溢出绝对可以肯定这些链接是代码。 Sorry for posting as code. 抱歉,无法以代码形式发布。

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

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