简体   繁体   English

我如何从外部页面获取整个元素?

[英]how can i obtain an entire element from an external page?

how can i get a 我如何获得

<table class="precios"> 

from another url (www.example.com) in HTML format?? 从另一个URL(www.example.com)转换为HTML格式? because with DOM i can obtain the table but in an array mode. 因为有了DOM,我可以在数组模式下获取表。

Thank you everyone for helping 谢谢大家的帮助

Assuming you have no cross-domain issues, you can use .load() for that: 假设您没有跨域问题,则可以使用.load()

$container.load('http://www.example.com/path/to/page table.precios');

Whereby $container is a jQuery object where you want to "save" the table into. $container是一个jQuery对象,您要将表“保存”到其中。

Within PHP you would solve it this way: 在PHP中,您可以通过以下方式解决它:

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile('http://www.example.com/path/to/page.html');
libxml_clear_errors();
$xp = new DOMXPath($doc);
$table = $xp->query('//table[@class="precios"]')->item(0);

echo $doc->saveHTML($table);

Make a AJAX request to that page and use .find() to get the element 向该页面发出AJAX请求,并使用.find()获取元素

$.ajax( {
  url: myUrl,
  success: function(html) {
    table = $(html).find(".precious");
    }
});

您还可以使用cURL(如果已安装)并在php中使用DOMDocument类

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

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