我正在尝试使用PHP和cURL从外部网站中提取一个元素。
我试图从中提取内容的网站的链接是: http ://www.stayclassy.org/fundraise?fcid=231864
我要定位的元素是
顶部右列中列表项“Raised So Far”下的数字值(现在值是 10 美元)。这是我用来提取数据的代码:
定义(“目标”,“ http://www.stayclassy.org/fundraise?fcid=231864 ”);
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, TARGET); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); if(!($results = curl_exec($curl))) { print("{ \"total\": \"$0.00\" }"); return; } $pattern = '/\<li class="goalTitle"\> \$(.+?) \<\/li\>\<\/a\>/'; preg_match_all($pattern, $results, $matches); $total = $matches[1][0]; $total = str_replace(",", "", $total); printf("{ \"total\": \"$%s\" }", formatMoney($total, true)); function formatMoney($number, $fractional=false) { if ($fractional) { $number = sprintf('%.2f', $number); } while (true) { $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number); if ($replaced != $number) { $number = $replaced; } else { break; } } return $number; }
我遇到的问题是我定位的列表项/元素没有唯一的 ID 或类。事实上,美元金额位于没有类别的单独列表项中。
我想知道如何使用上面的代码定位无序列表中的特定列表项,特别是当它没有类时。有任何想法吗?