0

我尝试从 html 页面的外部源中提取4G Network实际的整个行,LTE 700 MHz Class 17 / 1700 / 2100 - for AT&T但在其他情况下,它可能会更加不同LTE 850 / 900 / 1700 / 2100

我的结果的标题始终是 stable( 4G Network) 并且它是 under<td class="ttl">并且结果是在<td class="nfo">class 下,但是它们都在 under <tr>,所以我认为基于 titlettl class可以阅读基于下的内容nfo class

这是外部html的来源:

<tr>
<th rowspan="8" scope="row">General</th>
<td class="ttl"><a href="network-bands.php3">2G Network</a></td>
<td class="nfo">CDMA 800 / 1900 </td>
</tr><tr>
<td class="ttl">&nbsp;</td>
<td class="nfo">GSM 900 / 1800 / 1900 </td>
</tr>
<tr>
<td class="ttl"><a href="network-bands.php3">3G Network</a></td>
<td class="nfo">HSDPA 2100 </td>
</tr>
<tr>
<td class="ttl">&nbsp;</td>
<td class="nfo">CDMA2000 1xEV-DO </td>
</tr>
<tr>
<td class="ttl"><a href="network-bands.php3">4G Network</a></td>
<td class="nfo">LTE 700 MHz Class 17 / 1700 / 2100 - for AT&amp;T</td>
</tr><tr>
<td class="ttl"><a href="glossary.php3?term=sim">SIM</a></td>
<td class="nfo">Micro-SIM</td>
</tr><tr>
<td class="ttl"><a href="#" onclick="helpW('h_year.htm');">Announced</a></td>
<td class="nfo">2012, October</td>
</tr>

这是我使用的代码:

<?php
include_once('/simple_html_dom.php');
$dom = file_get_html("http://www.externalsite.com/pantech_vega_no_6-5268.php");
// alternatively use str_get_html($html) if you have the html string already...
 foreach ($dom->find('td[class=nfo]') as $node)
{
$result = $node->innertext;
$bresult = explode(",", $result);
echo $bresult[0];
} 
?>

我的代码的结果是这样的:

CDMA 800 / 1900 GSM 900 / 1800 / 1900 HSDPA 2100 CDMA2000 1xEV-DO LTE 700 MHz Class 17 / 1700 / 2100 - for AT&T Micro-SIM 2012, October

4

3 回答 3

1

如果您只想获得 4G 网络,您应该这样做:

<?php
include_once('/simple_html_dom.php');
$dom = file_get_html("http://www.gsmarena.com/pantech_vega_no_6-5268.php");
foreach ($dom->find('tr') as $node) {
    if (is_a($node->children(0), 'simple_html_dom_node')) {
        if ($node->children(0)->plaintext == "4G Network") {
            echo $node->children(1)->plaintext;
        }
    }
}
?>
于 2013-02-02T00:04:11.613 回答
0

您正在遍历所有结果,而您只想要第一个结果。所以不要循环并得到第一个结果:

$results = $dom->find('td[class=nfo]');
$node = reset($results);    // get the first element of the array
//the rest of your code:
$result = $node->innertext;
// etc.
于 2013-02-01T22:18:57.013 回答
0

你只想要第一个结果,这样你就可以在找到它后停下来得到你的结果

foreach ($dom->find('td[class=nfo]') as $node){
   $result = $node->innertext;
   break;
} 
于 2013-02-01T22:23:15.117 回答