1

我需要根据 URL 的结尾使用 simple_html_dom 获取 URL。URL 没有使其唯一的特定类。它唯一的独特之处在于它以一组特定的数字结尾。我只是想不出正确的语法来获取特定的 URL 然后打印它。

有什么帮助吗?

例子:

<table class="findList">
<tr class="findResult odd"> <td class="primary_photo"> <a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a> </td>

那是表格开头的代码。第一个 href 是我想要获取的。该表继续提供更多链接等,但这与我想要的无关。

4

3 回答 3

1

对于第一个带有以 1 结尾的 href 的 a:

$dom->find('a[href$="1"]', 0);
于 2013-02-15T09:00:23.943 回答
0

您可以简单地使用 DOMdocument

<?php 
$html = '
<table class="findList">
<tr class="findResult odd"> 
    <td class="primary_photo"> 
        <a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a> 
    </td>
';


$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
    if($td->getAttribute('class') == 'primary_photo'){
        $a = $td->getElementsByTagName('a')->item(0)->getAttribute('href');
    }
}
echo $a; // title/tt0080487/?ref_=fn_al_tt_1



//Or if your looking to get the img tag
$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
    if($td->getAttribute('class') == 'primary_photo'){
        $a = $td->getElementsByTagName('img')->item(0)->getAttribute('src');
    }
}

echo $a; // http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg
?>
于 2013-02-13T22:29:13.437 回答
0

假设您将 html 放在名为“tables.html”的文件中,这将起作用。它读取文件,找到所有“a”链接,将它们放入数组中,第一个 ($anchors[0]) 就是您想要的。然后你用 $anchors[0]->href 从中获取 href。

$html = new simple_html_dom(); 

$html->load_file('tables.html');

$anchors = $html->find("a");

echo $anchors[0]->href;
于 2013-02-14T17:14:28.153 回答