从外部站点(PHP,XPATH)检索div的内容?

时间:2014-06-18 00:40:39

标签: php dom xpath

我正在尝试使用PHP和xPath从外部站点检索和回显div的内容。

这是摘录自页面,显示相关代码:

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head><title>Handbags - Clutches - Kara Ross New York</title></head>
  <body>
    <div id="Container">
      <div id="AjaxLoading">...</div> ...
      <div id="Wrapper">
        <div class="productlist-page"> ...
          <div class="Content Wide " id="LayoutColumn1"> ...
            <div align="center">
              <div class="Block CategoryContent Moveable Panel" id="CategoryContent">
                <form name="frmCompare" id="frmCompare">
                  <table><tr><td valign="top">...</td>
                      <td valign="top">
                        <ul class="ProductList ">
                         <li class="Odd">
                           <div class="ProductImage QuickView" data-product="261">
                             <a href="http://www.kararossny.com/electra-clutch-in-oil-spill-lizard-and-hologram-with-gunmetal-hardware-and-hematite/">
                               <img src="http://cdn2.bigcommerce.com/n-arxsrf/t0qdc/products/261/images/1382/electra_oil_spill__08182.1402652812.500.375.jpg?c=2" alt="Kara Ross Electra Clutch in Oil Spill Lizard and Hologram with Gunmetal Hardware and Hematite Gemstone on Closure"/>
                             </a>
                           </div>
                           <div class="ProductDetails">...</div>
                           <div class="ProductPriceRating">...</div>
                           <div class="ProductCompareButton" style="display:none">...</div>
                           <div class="ProductActionAdd" style="display:none;">...</div>
                         </li>
                        </ul>
                      </td>
                      <td valign="top" align="center">...</td>
                    </tr>
                  </table>
                  <div class="product-nav btm"> ... </div>
                </form>
   ...

到目前为止,这是我的代码:

$url = 'http://www.kararossny.com/clutches/?sort=featured&page=1';

$dom = new DOMDocument;
@$dom->loadHTMLFile($url);

$xpath = new DOMXpath($dom);
$elements = $xpath->query('//div[class="ProductImage QuickView"]');

foreach($elements[0] as $child) {
   echo $child . "\n";
}

我想要的链接页面输出是:

<a href="http://www.kararossny.com/electra-clutch-in-oil-spill-lizard-and-hologram-with-gunmetal-hardware-and-hematite/">
    <img src="http://cdn2.bigcommerce.com/n-arxsrf/t0qdc/products/261/images/1382/electra_oil_spill__08182.1402652812.500.375.jpg?c=2" alt="Kara Ross Electra Clutch in Oil Spill Lizard and Hologram with Gunmetal Hardware and Hematite Gemstone on Closure"/>
</a>

知道我做错了什么吗?我认为我的xpath可能是错的,但我不确定。

谢谢!

3 个答案:

答案 0 :(得分:4)

您忘记在课程上添加@,在查询末尾添加a,因为要定位该链接。之后,使用saveHTML()来获取它。考虑这个例子:

$url = 'http://www.kararossny.com/clutches/?sort=featured&page=1';
$dom = new DOMDocument();
@$dom->loadHTMLFile($url);
$xpath = new DOMXpath($dom);
$elements = $xpath->query('//div[@class="ProductImage QuickView"]/a');
$link = $dom->saveHTML($elements->item(0));
echo $link;

答案 1 :(得分:3)

是的,你的XPath有点偏。

在XPath中,要按属性值过滤元素,必须在属性名称的开头使用@。所以你的XPath应该如下:

//div[@class="ProductImage QuickView"]

答案 2 :(得分:2)

有三个原因可能导致您无法选择所需的代码。

1 - 要在XPath谓词中选择class属性,您需要使用属性轴。使用attribute::@符号为属性名称添加前缀。所以你应该使用

@class

选择类属性。

2 - XPath表达式由一个或多个步骤组成。每个步骤都定义了一个上下文,它限制了下一步的范围。 last 步骤包含您选择的集合。由于 last 步骤为div,因此您实际上选择的是div,而不是a。您应该使用以下表达式来选择a节点及其内容:

//div[@class="ProductImage QuickView"]/a

3 - 最后,您的页面有一个默认名称空间声明

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

这将要求您注册忽略它使用通配符选择您的元素(不是通过名称,而是使用*)。大多数XPath API不会自动设置默认命名空间,如果没有使用命名空间来限定XPath选择器,它会将未加前缀的元素视为属于 no namespaces 。这意味着如果您尝试使用表达式<div>选择//div,则可能会获得一个空集。如果您没有选择任何内容,请尝试忽略名称空间,如下所示:

//*[local-name()='div'][@class="ProductImage QuickView"]/*[local-name()='a']
相关问题