dongzhiji0814 2015-09-08 17:05
浏览 45

无法使用xpath(url)访问文本的值

I am not able to get text. I need the price <meta itemprop="price" content="28.99"/>

Tried with this XPath:

    <?php
header('Content-Type: text/html; charset=utf-8'); 

$urlCT = "https://www.instant-gaming.com/es/834-comprar-key-steam-pro-evolution-soccer-2016/";

$ch = curl_init($urlCT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cl = curl_exec($ch);

$dom = new DOMDocument();
@$dom->loadHTML($cl);

$xpath = new DOMXpath($dom); 
$xpath->registerXpathNamespace('xhtml' , 'http://www.w3.org/1999/xhtml');
$eltitulo = $xpath->query('//xhtml:meta[@itemprop=\"price\"]/@content'); 
$titulo = $eltitulo->item($x)->nodeValue;
echo $titulo;
?>

How I can get the price xpath? (28.99€)

Thank you.

  • 写回答

1条回答 默认 最新

  • dtwncxs3547 2015-09-08 17:13
    关注

    If you look at the top of the page source, you'll see

    <html xmlns="http://www.w3.org/1999/xhtml" ...>
    

    This default namespace declaration says that every descendant of this element that doesn't have an explicit namespace prefix will be in the http://www.w3.org/1999/xhtml namespace. That includes the <meta> elements.

    Therefore you need to declare a prefix (e.g. xhtml) for this namespace, and use it in your XPath expression:

    $xpath->registerXpathNamespace('xhtml' , 'http://www.w3.org/1999/xhtml');
    $eltitulo = $xpath->query('//xhtml:meta[@itemprop=\"price\"]/@content'); 
    

    (Note that attributes are not affected by the default namespace, so @itemprop for example is not in any namespace, and doesn't need the xhtml prefix.)

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决