[英]Load external page and use it's elements like I would use them normally?
Can I load an external page using Javascript and convert it to a DOM structure so I can scrape it like I would to it normally? 我可以使用Javascript加载外部页面并将其转换为DOM结构,以便像通常那样刮取它吗?
Bad explanation, but code says more than thousand words, I think. 不好的解释,但是我认为代码可以说上千个单词。 ;)
;)
foobar = loadExternalPage('foobar.com');
foobar = convertToDOM(foobar);
headers = foobar.getElementsByClassName('header');
Thank you! 谢谢!
If the external page is on the same domain, then yes you can using XMLHttpRequest
, then treating the response as HTML. 如果外部页面在同一域中,则可以使用
XMLHttpRequest
,然后将响应视为HTML。 Alternatively, load it into an iframe and access the resulting contentDocument
. 或者,将其加载到iframe中并访问生成的
contentDocument
。
For a page on another domain, however, it's a bit more complicated. 但是,对于另一个域上的页面,则要复杂一些。 You may want to look at PHP's
DOMDocument
, which you can use to parse HTML from any domain, and even pass it back to JavaScript if you make an AJAX call to your PHP script. 您可能需要查看PHP的
DOMDocument
,可以用来从任何域解析HTML,如果对PHP脚本进行AJAX调用,甚至可以将其传递回JavaScript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.