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

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

谢谢!

参考方案

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

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

@class

选择class属性。

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

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

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

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

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

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

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

PHP:如何从JSON数组获取属性? - php

我有以下JSON数组:使用PHP,如何从上面的JSON数组中获取geometery->location->lat&lng值?例如(伪代码):<?php $json = { "status": "OK", "results": [ { "types": [ �…

PHP:从函数返回值并直接回显它? - php

这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…

使用Ajax呈现html表 - php

我想知道如何实现以下项目其实我有一个php代码,可以渲染一张桌子<table id = "oldata" class ="table table-bordered"> <thead> <tr class ="success"> <th class="…

提交表单后显示模式对话框 - php

提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…