我似乎无法通过我的xpath查询来查找它。
$pathTemplate = "https://www.google.com/finance?q=";
echo "Enter the stock symbol of the stock you want to check\n";
$foo = fgets(STDIN);
$html = new DOMDocument();
$html -> loadHTML($pathTemplate . $foo);
$xpath = new DOMXPath($html);
$price = $xpath -> query("//span[@class = 'pr']/span/text()");
echo $price -> item(0);
答案 0 :(得分:0)
使用xpath时,应该规范化(总是)类,以便能够正确处理class="class1 class2"
标记。
获得所需内容的最佳方式是首先定位ID="price-panel"
。
在完整的工作代码段下方,您忘记了file_get_contents
,因此您尝试将网址解析为HTML而不是实际的HTML; - )
$pathTemplate = "https://www.google.com/finance?q=";
echo "Enter the stock symbol of the stock you want to check\n";
$foo = fgets(STDIN);
$html = file_get_contents($pathTemplate . $foo);
$dom = new DOMDocument();
@$dom -> loadHTML($html);
$xpath = new DOMXPath($dom);
$price = $xpath -> query(" //div[@id='price-panel']//span[contains(concat(' ', normalize-space(@class), ' '), ' pr ')]/span");
$price = $price->item(0)->nodeValue;
echo $price;