简体   繁体   English

加载外部页面并使用它的元素,就像我通常会使用它们一样?

[英]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.

相关问题 在页面加载时捕获元素,删除它们,然后能够使用初始内容 - Capture elements on page load, remove them, then be able to use initial contents 我想停止使用空格键向下滚动页面以将其与其他功能一起使用 - I would like to stop the page scrolling down with the space bar to use it with another function 我想将 HTML 元素添加到 exupero 的 saveSvgAsPng 函数中 - I would like to add HTML elements to the exupero's saveSvgAsPng functions 我应该使用iframe加载外部html吗? - Should I use iframes to load external html? 是否可以使用像 js 之类的东西来加载 HTML 中的导航栏? 它通过 Github Pages 托管,因此服务器端无法正常工作 - Is it possible to use like js or something to load a navbar in HTML? It's hosted through Github Pages, so nothing server side would work 如何获取这些javascript值并将其用于图像源? - How would I get these javascript values and use them in an image source? 页面加载后如何加载外部js文件并使用同一js中的函数 - how do i load an external js file after the page has loaded AND use a function from that same js jQuery load()来自不同页面的图像,并在脚本中使用它们 - jQuery load() images from different page and use them in script 我正在使用CSS隐藏单选按钮,但是我想使用Javascript将它们显示在一页上 - I am using CSS to hide radio buttons, but I would like to display them on one page using Javascript 我想使用JQuery来验证表单而不使用.inArray()和.val() - I would like to use JQuery to validate a form unsing .inArray() and .val()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM