【发布时间】:2017-04-08 03:04:23
【问题描述】:
以下是从元素中提取单个值的工作代码(基本上是获取当前汇率值。
$target_url = file_get_contents('http://www.bsp.gov.ph/statistics/sdds/exchrate.htm');
$new_dom_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($target_url)){ //check if target_url is actually returned
$new_dom_doc->loadHTML($target_url);
libxml_clear_errors(); //remove errors from yucky target_url
$xpath = new DOMXPath($new_dom_doc);
echo $xpath->query('//td[@class="xl1257110"]')->item(0)->nodeValue;
}
它工作正常,但唯一的问题是类名 //td[@class="xl1257110"] 不是静态的,因此只要类名在随机时间段后更改为随机值,代码就会中断。
有什么办法可以绕过这个问题吗?
【问题讨论】:
-
你能给我举个例子,它不适合哪个类吗?
-
好像已经变了。你能告诉我你到底需要哪个速率,因为我现在看不到
xl1257110类的元素 :)
标签: php xpath web-scraping domdocument