【发布时间】: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