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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失