这是表dom输出:
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<th>Валута</th>
<th>Код</th>
<th>За единица<br />валута</th>
<th>Фиксинг на БНБ</th>
<th>Курс купува</th>
<th>Курс продава</th>
</tr>
<tr>
<td>Австралийски долар</td>
<td><em>AUD</em></td>
<td align="center">1</td>
<td>1.328960</td>
<td>1.29340</td>
<td>1.35670</td>
</tr><tr>
<td>Британска лира</td>
<td><em>GBP</em></td>
<td align="center">1</td>
<td>2.325380</td>
<td>2.26920</td>
<td>2.37970</td>
</tr><tr>
<td>Евро</td>
<td><em>EUR</em></td>
<td align="center">1</td>
<td>1.955830</td>
<td>1.94900</td>
<td>1.95850</td>
</tr><tr>
<td>Канадски долар</td>
<td><em>CAD</em></td>
<td align="center">1</td>
<td>1.354830</td>
<td>1.32380</td>
<td>1.38890</td>
</tr><tr>
<td>Швейцарски франк</td>
<td><em>CHF</em></td>
<td align="center">1</td>
<td>1.800950</td>
<td>1.75730</td>
<td>1.84140</td>
</tr><tr>
<td>Щатски долар</td>
<td><em>USD</em></td>
<td align="center">1</td>
<td>1.775770</td>
<td>1.73710</td>
<td>1.81010</td>
</tr><tr>
<td>Японска йена</td>
<td><em>JPY</em></td>
<td align="center">100</td>
<td>1.673650</td>
<td>1.63180</td>
<td>1.71330</td>
</tr><tr>
<td>Датска крона</td>
<td><em>DKK</em></td>
<td align="center">10</td>
<td>2.629160</td>
<td>2.56400</td>
<td>2.69070</td>
</tr><tr>
<td>Норвежка крона</td>
<td><em>NOK</em></td>
<td align="center">10</td>
<td>2.084060</td>
<td>2.03290</td>
<td>2.13250</td>
</tr><tr>
<td>Шведска крона</td>
<td><em>SEK</em></td>
<td align="center">10</td>
<td>2.059220</td>
<td>2.00700</td>
<td>2.10550</td>
</tr><tr>
<td>Руска рубла</td>
<td><em>RUB</em></td>
<td align="center">100</td>
<td>2.751570</td>
<td>2.42030</td>
<td>3.11320</td>
</tr><tr>
<td>Нова румънска лея</td>
<td><em>RON</em></td>
<td align="center">10</td>
<td>4.384380</td>
<td>4.17110</td>
<td>4.61270</td>
</tr>
</tbody>
</table>
我没有兴趣仅使用<em>GBP</em>
行和最后一个<td></td>
文本。
我不需要的所有其他人。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('app/Mage.php');
Mage::app();
$url = 'https://www.fibank.bg/bg/valutni-kursove/page/461';
$curl = curl_init();
curl_setopt($curl, CURLOPT_COOKIE, "ChosenSite=www; SportsDirect_AnonymousUserCurrency=GBP; language=en-GB");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl, CURLOPT_VERBOSE, true);
$str = curl_exec($curl);
curl_close($curl);
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($str);
$xpath = new \DOMXpath($doc);
$value = $xpath->query('//td[em="GBP"]/parent::tr/td[last()]')->item(0)->nodeValue;
print_r($value);
此代码完全无效。 能帮我解决一下吗?
提前致谢!
答案 0 :(得分:1)
使用XPath parent
轴和last()
谓词:
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($str);
$xpath = new \DOMXpath($doc);
$value = $xpath->query('//td[em="GBP"]/parent::tr/td[last()]')->item(0)->nodeValue;
print_r($value); // "2.37970"