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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭