从表php获取链接

时间:2015-01-21 11:17:47

标签: php html

如何从表中获取链接并使用php保存在file.txt中:

        

<TABLE width="600" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed">
                    <TR>                
                        <TD width="15"></TD>
                        <TD width="570" valign="top">

                            <TABLE width="570" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed">
                            <TR>

                            <TD width="190" valign="top">

                                <TABLE width="190" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed">



                                        <TR height="98">
                                            <TD width="190" align="center" valign="top"><A HREF="http://mylink.com/1" class="opacityit"><IMG SRC="http://mylink.com/1/784.jpg" title="test1" title="test1" BORDER=0 style="cursor:hand" /></A></TD>
                                        </TR>
                                        <TR height="2">
                                            <TD width="190"></TD>
                                        </TR>

                                        <TR>
                                            <TD width="190" align="center" Class="text6"><A HREF="http://mylink.com/1" class="Link8" title="test1"><h2 style="color:#000"><font size=2>test1</font></h2></A></TD>
                                        </TR>

                                </TABLE>

                            </TD>

                                <TABLE width="190" border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed">

                                        <TR height="98">
                                            <TD width="190" align="center" valign="top"><A HREF="http://mylink.com/2" class="opacityit"><IMG SRC="http://mylink.com/2/784.jpg" title="test2" title="test2" BORDER=0 style="cursor:hand" /></A></TD>
                                        </TR>
                                        <TR height="2">
                                            <TD width="190"></TD>
                                        </TR>

                                        <TR>
                                            <TD width="190" align="center" Class="text6"><A HREF="http://mylink.com/2" class="Link8" title="test2"><h2 style="color:#000"><font size=2>test2</font></h2></A></TD>
                                        </TR>

                                </TABLE>

                            </TD>

$ html = file_get_contents($ urlcontent);

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//tr");



for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');

echo $url.'<br />';
}

如何从表中获取链接并使用php

将其保存在file.txt中

我只想获得表格的链接

2 个答案:

答案 0 :(得分:0)

这将为您提供HTML中的所有链接:

$html = file_get_contents($urlcontent);

$dom = new DOMDocument();
@$dom->loadHTML($html);

$links = array();
foreach($dom->getElementsByTagName('a') as $node)
    $links[] = $node->getAttribute('href');

print_r($links);

如果你想只获得表格中的链接:

$html = file_get_contents($urlcontent);

$dom = new DOMDocument();
@$dom->loadHTML($html);

$links = array();
foreach($dom->getElementsByTagName('table') as $table)
    foreach($table->getElementsByTagName('a') as $node){
        $href = $node->getAttribute('href');
        if(!in_array($href, $links))
            $links[] = $href;
    }

print_r($links);

答案 1 :(得分:0)

您可以在小写html之后应用以下代码作为字符串

$matches = array();
preg_match('/<a\s[^>]*href=\"([^\"]*)\"/', $url, $matches);