dpb35161 2013-09-09 20:06
浏览 31
已采纳

从外部URL获取div内容 - 最好的方法 - PHP? DOM? jQuery的?

I am trying to get the contents of a div from the id name.

Here is the div I am trying to get: <div id="article-body"> ... </div>

However, this is on another external website so it has to be called with a www or an http:// etc...

I am sure it's possible. Just not sure if I should use PHP, DOM or jQuery etc..

I am thinking this code should be possible to do in a few lines. Just don't know what is the best method. Thanks for the tips or ideas.

UPDATE: It was suggested this was a duplicate question. It is not. I have used the code from the suggested duplicate question below and it does not work.

Here is one of the errors: Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID changeRegionForm already defined in Entity, line: 85 in /home/content/w/i/s/wisdom33/html/testing/getDivExternalWebsite.php on line 14

Here is a link to the code: http://massmediamail.com/testing/getDivExternalWebsite.php

Here is the code:

<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>
  • 写回答

2条回答 默认 最新

  • doushu0591 2013-09-10 00:21
    关注

    The answer is in http://simplehtmldom.sourceforge.net/

    And once you download the code from the above url and place it in the appropriate place and link to it correctly then this code at the bottom works just perfect for what I asked above.

    <?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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝