【问题标题】:Get specific html content from other site with PHP使用 PHP 从其他站点获取特定的 html 内容
【发布时间】:2013-03-09 19:12:51
【问题描述】:

我想尝试获取我在 IcheckMovies 网站上查看的最新电影并将其显示在我的网站上。我不知道怎么做,我已经阅读了 php_get_contents() 然后获取了一个元素,但是我想要的特定元素在 DOM 结构中相当深入。它在一个 div 中的一个 div 中的一个列表中的一个 ...

所以,这是我想从中获取内容的链接:http://www.icheckmovies.com/profiles/robinwatchesmovies,我想获取列表中电影的第一个标题。

提前非常感谢!

编辑:

所以使用 file_get_contents() 方法

<?php
$html = file_get_contents('http://www.icheckmovies.com/profiles/robinwatchesmovies/');
echo $html;
?>

我得到了这个 html 输出。现在,我只需要获取'Smashed',以便在列表中的 div 中的 div 中的 h3 中的 href 链接的内容。这是我不知道如何获得它的地方。

...
<div class="span-7">
<h2>Checks</h2>
<ol class="itemList">
<li class="listItem listItemSmall listItemMovie movie">
<div class="listImage listImageCover">
<a class="dvdCoverSmall" title="View detailed information on Smashed (2012)" href="/movies/smashed/"></a>
<div class="coverImage" style="background: url(/var/covers/small/10/1097928.jpg);"></div>
</div>
<h3>
<a title="View detailed information on Smashed (2012)" href="/movies/smashed/">Smashed</a>
</h3>
<span class="info">6 days ago</span>
</li>
<li class="listItem listItemSmall listItemMovie movie">
<li class="listItem listItemSmall listItemMovie movie">
</ol>
<span>
</div>
...

【问题讨论】:

  • ID 元素在 html 中的深度无关紧要……元素的 ID 应该是唯一的。发布您尝试使用的代码
  • 嗨,这很模糊,您基本上已经提到了最常见的解决方案 - file_get_contents() 并使用 HTML parser. 解析 DOM 有很多示例如何在以下情况下执行此操作例如谷歌搜索Get specific html content from other site with PHP。您是否对此感到困惑?
  • 很抱歉,我不太清楚。我已添加代码,希望对您有所帮助。
  • 可以通过在文本周围放置重音`字符来突出显示您想要的html

标签: php dom file-get-contents


【解决方案1】:

有一些库可以帮助你! 很久以前,我用于相同目的的一个是:http://simplehtmldom.sourceforge.net/

希望对你有帮助!

【讨论】:

    【解决方案2】:

    按照步骤来实现这一点

    第 1 步:-

    首先使用 php 文件中的 file_get_contents 获取内容

    例如:getcontent.php

    <?php
    
    echo file_get_contents("http://www.icheckmovies.com/movies/checked/?user=robinwatchesmovies ");
    
    ?>
    

    第二步:-

    使用 ajax 调用调用上述脚本并将内容添加到 html 中的可见性隐藏字段。

    例如:

    $('#hidden_div').html(response);
    

    html:-

    <html>
    <body>
    <div id='hidden_div' style='visibility:hidden'>
    </div>
    </body>
    </html>
    

    第三步:-

    现在提取你想要的 id。

    【讨论】:

    • 我认为不需要 ajax 或隐藏的 div。正如您在我编辑的第一篇文章中看到的那样,我已经尝试了 file_get_contents() 方法。非常感谢您的帮助!
    • 嘿@Robin 我的意思是..你通过了第一步你创建了一个php文件写?现在使用 ajax 调用在 html 页面中获取这个 php 脚本的响应,然后按照剩余的步骤 $('#hidden_​​div h3 a').attr('href');
    【解决方案3】:

    您所要求的称为网络抓取,几个月前我已经这样做了,过程是这样的,

    • 向您需要内容的站点创建HttpRequest,检查 php class for it
    • 使用 DOM 解析库来处理下载的页面(它会在 html 中),simple HTLM DOM 将是一个不错的选择
    • 提取所需信息

    这里有一些教程,

    SO 帖子:

    最好的谷歌是你的朋友,只需搜索“PHP scraping”

    【讨论】:

    • 谢谢,我会调查一下!
    • @Robin 不要忘记标记为答案,如果此答案对您有帮助,请点赞!
    猜你喜欢
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2011-09-21
    • 2019-05-13
    相关资源
    最近更新 更多