【问题标题】:Detect mp3 links on an html page检测 html 页面上的 mp3 链接
【发布时间】:2015-09-24 12:38:38
【问题描述】:

我想从 html 网页中查找并打印所有 mp3 链接。

<a href="http://example1.com">Test 1</a>
<a href="http://example2.com/page.html">Test 2</a>
<a href="http://example3.com/link/file2.mp3">mp3 link 2</a>
<a href="http://example3.com/link/file3.mp3">mp3 link 3</a>

我现在使用 php 编写了 HTML 代码,我想在我的网页中打印两个 mp3 链接,所以请帮助我。我认为 DOM 可以在这里帮助我,但我不知道如何?您可以使用 DOM 或其他。谢谢你

让我们清楚-

$dom = new DomDocument();
$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $urls, $matches, PREG_SET_ORDER))
{ foreach($urls as $match)
{// $match[2] = link address
// $match[3] = link text}
}

这将打印所有链接,但我想打印一个具有 .mp3 远征的链接。我想现在很清楚了。所以请帮忙

【问题讨论】:

  • 另请注意,我在这里使用 2 个 mp3 链接。但我会获取网页的整个 html,所以我不会知道他们有多少 mp3 链接的信息。所以请指导我检测网页中的所有 mp3 链接。这可能有助于找到link

标签: php html dom


【解决方案1】:

这样做...

<?php
$html='<a href="http://example1.com">Test 1</a>
<a href="http://example2.com/page.html">Test 2</a>
<a href="http://example3.com/link/file2.mp3">mp3 link 2</a>
<a href="http://example3.com/link/file3.mp3">mp3 link 3</a>';
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $tag) {
        if(strpos($tag->getAttribute('href'),'.mp3')!==false)
        {
            echo $tag->getAttribute('href')."<br>";
        }

}

输出:

http://example3.com/link/file2.mp3
http://example3.com/link/file3.mp3

【讨论】:

  • 该死!以DOMDocument 为例!
  • 在这里我可以使用这样的 DOM 获取链接的 html - file_get_contect('example.com/ddd.html');或使用 curl
  • 你可以做任何一种方式..只需在loadHTML($yourhtmlcontentfromcURLorfile_get_contents);中传递那个源代码
  • 你能帮我找到一个网站上的所有 mp3 链接吗?
  • 使用该技术我们只能在一个页面上找到,但我想从整个网站获取。
【解决方案2】:

使用 CSS3,您可以使用像这样的选择器选择此类元素

a[href$=mp3]

在 jQuery 中类似的东西就是你要找的东西。

我在这里创建了一个小提琴:http://jsfiddle.net/myTerminal/Q4D9n/ 该变量包含您要查找的链接数组。

【讨论】:

  • 在小提琴中,以下代码提醒所有网址:$.each(themp3Links, function(index, item){ alert($(item).attr("href")); });
  • 对不起朋友你不理解我的想法。请再次检查我的问题。我不想将 css 应用于他们。好的
  • 如果你按照小提琴,你会看到那里的脚本可以找到该页面上的所有 mp3 链接。后来我读到你想创建一个类似迷你网络爬虫的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
相关资源
最近更新 更多