Php 检测html页面上的mp3链接
我想从html网页中查找并打印所有mp3链接Php 检测html页面上的mp3链接,php,html,dom,Php,Html,Dom,我想从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/fi
<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>
我有HTML代码现在使用php我想打印我的网页中的两个mp3链接,所以请帮助我。我想多姆可以帮我,但我不知道怎么做?您可以使用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}
}
$dom=newdomdocument();
$dom->loadHTML($html);
$URL=$dom->getElementsByTagName('a');
$regexp=“]*href=(\”?)([^\“>]*?)\\1[^>]*>(.*);
if(preg_match_all(“/$regexp/siU“,$url,$matches,preg_SET_ORDER))
{foreach($url作为$match)
{//$match[2]=链接地址
//$match[3]=链接文本}
}
这将打印所有的链接,但我想打印一个链接,其中有.mp3。我想现在清楚了。因此,请使用CSS3帮助,您可以使用类似
a[href$=mp3]
jQuery中类似的东西正是您所需要的
我在这里创作了一把小提琴:
变量包含您正在查找的链接数组。请执行以下操作
<?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>";
}
}
还请注意,我在这里使用了2个mp3链接。但我会获取整个网页的html,这样我就不会知道他们有多少mp3链接了。所以请引导我检测网页上的所有mp3链接。这可能有助于在fiddle中找到,下面的代码提醒所有URL:
$。每个(Themm3links,function(index,item){alert($(item).attr(“href”);})对不起,朋友,你不理解我的想法。请再次检查我的问题。我不想对它们应用css。好的,如果你跟着小提琴走,你会看到里面的脚本可以找到该页面上所有的mp3链接。我后来读到你想创建一个像迷你网络爬虫一样的东西。该死!请给我一个DOMDocument
示例!在这里,我可以使用类似于-file_get_contect(')的DOM获取链接的html;或者使用卷发器,您可以采用任何一种方式。。只需在loadHTML($yourhtmlcontentfromcURLorfile\u get\u contents)中传递该源代码你能帮我找到一个网站上所有的mp3链接吗?使用这种技术我们只能在一个页面上找到,但我想从整个网站上获取。
http://example3.com/link/file2.mp3
http://example3.com/link/file3.mp3