简体   繁体   中英

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?? because with DOM i can obtain the table but in an array mode.

Thank you everyone for helping

Assuming you have no cross-domain issues, you can use .load() for that:

$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.

Within PHP you would solve it this way:

$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( {
  url: myUrl,
  success: function(html) {
    table = $(html).find(".precious");
    }
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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