簡體   English   中英

從外部html源自定義對象讀取

[英]Reading from external html source custom objects

我從外部html源讀取存在問題,我所要讀取的是我的自定義對象“ HSDPA 2100”,但我的實際代碼是從外部源讀取所有nfo類。

一段外部html:

<table cellspacing="0">
<tbody><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 850 / 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 800 </td>
</tr>
<tr>
<td class="ttl"><a href="glossary.php3?term=sim">SIM</a></td>
<td class="nfo">Mini-SIM</td>
</tr><tr>
<td class="ttl"><a href="#" onclick="helpW('h_year.htm');">Announced</a></td>
<td class="nfo">2013, January</td>
</tr>
<tr>
<td class="ttl"><a href="#" onclick="helpW('h_status.htm');">Status</a></td>
<td class="nfo">Coming soon. Exp. release 2013, February</td>
</tr>
</tbody></table><table cellspacing="0">
<tbody><tr>
<th rowspan="2" scope="row">Body</th>
<td class="ttl"><a href="#" onclick="helpW('h_dimens.htm');">Dimensions</a></td>
<td class="nfo">-</td>
</tr><tr>
<td class="ttl"><a href="#" onclick="helpW('h_weight.htm');">Weight</a></td>
<td class="nfo">&nbsp;</td>
</tr>

</tbody></table><table cellspacing="0">
<tbody><tr>
<th rowspan="4" scope="row">Display</th>
<td class="ttl"><a href="glossary.php3?term=display-type">Type</a></td>
<td class="nfo">TFT capacitive touchscreen, 16M colors</td>
</tr><tr>
<td class="ttl"><a href="#" onclick="helpW('h_dsize.htm');">Size</a></td>
<td class="nfo">1080 x 1920 pixels, 5.9 inches (~373 ppi pixel density)</td>
</tr>
<tr>
<td class="ttl"><a href="glossary.php3?term=multitouch">Multitouch</a></td>
<td class="nfo">Yes</td>
</tr>
<tr><td class="ttl">&nbsp;</td><td class="nfo">- Flux UX UI</td> 

我正在嘗試使用以下代碼:

  <?php
  include_once('/simple_html_dom.php');
  $dom = file_get_html("http://www.site.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;
$price = explode(",", $result);
echo $price[0];
} 
?>

我收到的是: CDMA 800 / 1900 GSM 850 / 900 / 1800 / 1900 HSDPA 2100 CDMA2000 1xEV-DO LTE 800 Mini-SIM2013Coming soon. Exp. r... etc CDMA 800 / 1900 GSM 850 / 900 / 1800 / 1900 HSDPA 2100 CDMA2000 1xEV-DO LTE 800 Mini-SIM2013Coming soon. Exp. r... etc

我想要的是HSDPA 2100但對於其他型號的手機,其價值可能是HSDPA 1900或其他,而HSPDA始終是穩定的,並且是第一位的。

所有td都具有相同的類名稱“ nfo”,並且您循環遍歷所有元素,因此所獲得的結果符合預期。

如果所需數據始終位於第三行,則可以填充數組而不是獲取變量,然后獲取第三值。 像這個$ result [2]

更新:如果HSDPA始終存在,只需對其進行檢查。

     <?php
      include_once('/simple_html_dom.php');
      $dom = file_get_html("http://www.site.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;
if (strpos($result, 'HSDPA') === false)
{
continue;
}
    $price = explode(",", $result);
    echo $price[0];
break;
    } 
    ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM