【问题标题】:PHP Crawl Specific Tab Content of External Website & Return hrefPHP抓取外部网站的特定标签内容并返回href
【发布时间】:2020-03-26 13:03:34
【问题描述】:

使用 PHP,我想检索外部网站中的特定元素。

外部网站是https://mcnmedia.tv/iframe/2684 我要检索的具体元素是“录音”选项卡中的第一个链接

例如第一个链接包含以下html;

<div class="small-12 medium-6 me column recording-item">
    <div class="recording-item-inner">
        <a class="small-12 column recording-name" href="/recordings/2435">
        <div class="info">
            <b>Mass</b><br>
            <small>26 Mar 2020</small>
        </div><i class="fa fa-play"></i></a>
    </div>
</div>

我想检索href 并在我的网站上显示一个直接链接,例如;

View Latest Recording - https://mcnmedia.tv/recordings/2435.

我有以下 PHP,但它没有按我的意愿工作,目前它只输出文本 (Mass 26 Mar 2020),我不确定如何获取实际的 href 链接地址?

<?php
$page = file_get_contents('https://mcnmedia.tv/iframe/2684');
@$doc = new DOMDocument();
@$doc->loadHTML($page);   
$xpath = new DomXPath($doc);
$nodeList = $xpath->query("//div[@class='recording-item-inner']");
$node = $nodeList->item(0);
// To check the result:
echo "<p>" . $node->nodeValue . "</p>";
?>

我怎样才能做到这一点?

【问题讨论】:

    标签: php html web-scraping file-get-contents


    【解决方案1】:

    您的 XPath 还不足以获取 href,您可以添加 /a/@href 表示在 &lt;a&gt; 标记内使用 href 属性...

    $nodeList = $xpath->evaluate("//div[@class='recording-item-inner']/a/@href");
    

    您可以简化这一点,使用 evaluate() 获取特定值并修改 XPath 以将属性作为字符串而不是节点获取...

    $href = $xpath->evaluate("string(//div[@class='recording-item-inner']/a/@href)");
    echo "<p>" . $href . "</p>";
    

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2022-11-22
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多