从远程网站提取 HTML 内容并显示在页面上

2024-01-04

我已经在这方面工作了一段时间了,但我很困惑。我试图从远程网站页面上的特定 div 中提取内容,然后将该 html 插入我自己网站上的 div 中。我知道您不能单独使用 jQuery 的 .ajax、.load 或 .get 方法来执行此类操作。

这是远程页面的 HTML:

<html>
    <body>
        <div class="entry-content">
            <table class="table">
                ...table #1 content...
                ...More table content...
            </table>
            <table class="table">
                ...table #2 content...
            </table>
            <table class="table">
                ...table #3 content...
            </table>
        </div>
    </body>
</html>

Goal:我正在尝试从远程页面的第一个表中获取 html。因此,在我的网站上,我希望获取以下 html 并将其放置在 id="fetched-html" 的 div 中:

<table class="table">
    ...table #1 content...
    ...More table content...
</table>

到目前为止,我的 PHP 函数处于以下位置:

<?php
function pullRaspi_SDImageTable() {
    $url = "http://www.raspberrypi.org/downloads";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curl);
    curl_close($curl);

    // Create new PHP DOM document
    $DOM = new DOMDocument;
    // Load html from curl request into document model
    $DOM->loadHTML($output);

    // Get 1st table
    $output = $DOM->firstChild->getElementsByTagName('table');

    return $output;
}
?>

最终结果在我的本地网站页面上应如下所示:

<div id="fetched-html">
    <table class="table">
        ...table #1 content...
        ...More table content...
    </table>
</div>

这是另一个 PHP 函数的可能性吗?

<?php
function pullRaspPi_SDImageTable() {
    // Url to fetch
    $url = "http://www.raspberrypi.org/downloads";

    $ch = curl_init($url);
    $fp = fopen("raspberrypi_sdimagetable.txt", "w");
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

    // Write html source to variable
    $rasp_sdimagetable = curl_exec($ch);

    // Close curl request
    curl_close($ch);

    return $rasp_sdimagetable;
}

// Then in the head of the html, add this jQuery:
<script type="text/javascript">
    $("#fetched-html").load("<?php pullRaspPi_SDImageTable(); ?> table.table:first");
</script>

问题是,这两个功能都不起作用。 :( 有什么想法吗?


从网站中提取 HTML 片段轻而易举简单的htmldom http://simplehtmldom.sourceforge.net/然后你可以做类似的事情:

function pullRaspi_SDImageTable() {
    $filename = '/tmp/downloads.html';  /// Where you want to cache the result
    $expiry = 600;  // 10 minutes
    $output = '';

    if (!file_exists($filename) ||  time() - $expiry > filemtime($filename)) {
        // There is no cache, so fetch the results from remote server
        require_once('simple_html_dom.php');
        $html = file_get_html('http://www.raspberrypi.org/downloads');
        foreach($html->find('div.entry-content table.table') as $elem) {
                $output .= (string)$elem;
        }

        // Store the cache
        file_put_contents($filename, $output);
    } else {
        // Pull the content from the cahce
        $output = file_get_contents($filename);
    }

    return $output;
}

这会给你table.table HTML

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从远程网站提取 HTML 内容并显示在页面上 的相关文章

随机推荐