从外部 url 获取 div 内容 - 最佳方法 - PHP?DOM?jQuery?

Pap*_*eau -1 html css php jquery dom

我试图从 id 名称获取 div 的内容。

这是我想要获得的 div: <div id="article-body"> ... </div>

但是,这是在另一个外部网站上,因此必须使用 www 或 http:// 等调用它...

我确定这是可能的。只是不确定我是否应该使用 PHP、DOM 或 jQuery 等。

我认为这段代码应该可以在几行中完成。只是不知道什么是最好的方法。感谢您的提示或想法。

更新:有人建议这是一个重复的问题。它不是。我使用了下面建议的重复问题中的代码,但它不起作用。

这是错误之一:警告:DOMDocument::loadHTML() [domdocument.loadhtml]: ID changeRegionForm already defined in Entity, line: 85 in /home/content/w/i/s/wisdom33/html/testing/getDivExternalWebsite第 14 行的 .php

这是代码的链接:http : //massmediamail.com/testing/getDivExternalWebsite.php

这是代码:

<html>

<body>
<?
$doc = new DomDocument;

// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://www.lifesitenews.com/news/second-madagascar-archbishop-criticizes-catholic-relief-services-full-trans'));

var_dump($doc->getElementById('article-body'));
?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Pap*_*eau 6

答案在http://simplehtmldom.sourceforge.net/

一旦您从上面的 url 下载代码并将其放置在适当的位置并正确链接到它,那么底部的这段代码就非常适合我上面的要求。

<?php

// Note you must download the php files from the link above 
// and link to them on this line below. 
include_once('../simple_html_dom.php');

$html = file_get_html('http://www.lifesitenews.com/news/second-madagascar-archbishop-criticizes-catholic-relief-services-full-trans');
$elem = $html->find('div[id=article-body]', 0);
echo $elem;

?>
Run Code Online (Sandbox Code Playgroud)