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



我正在尝试使用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可能是错的,但我不确定。

谢谢!

您忘记在类上添加@,并在查询的末尾添加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;

是的,您的XPath有点不对劲。

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

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

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

1—要在XPath谓词中选择class属性,需要使用属性轴。在属性名前加上attribute::@符号。所以你应该使用

@class

选择class属性。

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

//div[@class="ProductImage QuickView"]/a
最后,你的页面有一个默认的命名空间声明:
xmlns="http://www.w3.org/1999/xhtml"

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

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

最新更新