无法通过XPath获得正确的价值

时间:2018-07-31 18:06:57

标签: php html xpath

我正在使用src来收集数据并用作API。 重做它的用于获取信息的网站已经重做,有些工作正常,有些则没有。

PHP

protected $namexpath = ".//h1[contains(@itemprop,\"name\")]/a";

使用 HTML源

 <h1 itemprop="name" class="fn itemTitle">
    <a title="https://www.paginegialle.it/altopascio-lu/lotto-ricevitorie/lucky-planet-duro-anastasia-tabaccheria-ricevitori" href="https://www.paginegialle.it/altopascio-lu/lotto-ricevitorie/lucky-planet-duro-anastasia-tabaccheria-ricevitori">
        Lucky <strong>Planet</strong> - Duro Anastasia <strong>Tabaccheria</strong> Ricevitoria Lotto
    </a>
</h1>

但这不起作用:

PHP

protected $telephonexpath = ".//div[@class=\"hidden-phone-elem visiblePhone\"]/span";

HTML来源

<section itemscope="" itemtype="https://schema.org/LocalBusiness" class="vcard listElement   flFree " data-user="teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda" data-id="4" data-fl_free="true" data-cd_opec="GU01WAAW" data-cd_aggregazione="23787370" data-cd_id_sede="E57901ED-8833-A2AD-E040-A8C08D264C56">
<div class="container">
    <div class="row">
        <div class="col contentCol">
            <header>
                <div class="tabletOnlyBadge">
                </div>
                <h1 itemprop="name" class="fn itemTitle">
                    <a title="https://www.paginegialle.it/teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda" href="https://www.paginegialle.it/teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda">
                    <strong>Planet</strong> Cafe' di Tozzi Iolanda
                    </a>
                </h1>
                <span class="itemSubtitle">
                </span>
                <div>
                    <span class="itemAddress">
                        <span class="adr" itemprop="location" itemscope="" itemtype="https://schema.org/Place">
                            <div class="street-address">
                                <span>105, Via Roma</span> -
                                <span class="postal-code">81030</span> 
                                <span class="locality">Teverola</span> <span class="region">(CE)</span>
                            </div>
                            <div style="display: none;">
                                <span>40.99494</span>
                                <span>14.2077</span>
                            </div>
                        </span>
                    </span>
                </div>
            </header>
            <div>
                <div class="hidden-phone-wrapper">
                    <span class="custom-label"></span>
                    <div class="hidden-phone-elem">
                        <div class="btn btn-yellow btn-show-phone" data-pag="mostra telefono" data-context="listing">
                            <span>MOSTRA TELEFONO</span>
                        </div>
                        <div class="btn btn-hidden-phone">
                            <span class="phIco "></span>
                            <span class="phone-label">081 5034556</span>
                        </div>
                    </div>
                </div>
                <div class="itemGeoLinks">
                    <ul>
                    </ul>
                </div>
                <div class="itemPayoff">
                    <p class="payoff-title">
                        <a class="cat" href="//www.paginegialle.it/ricerca/cat/008647000" rel="nofollow"><strong>Tabacchi</strong>, sigarette e sigari - produzione e commercio</a>
                    </p>
                    <p itemprop="description" class="payoff-txt"></p>
                </div>
                <div class="itemInfoTags">
                </div>
            </div>
        </div>
        <div class="col-3 logoCol">
            <div class="itemRating">
                <a rel="nofollow" href="//www.paginegialle.it/teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda/commenti#scrivi">
                    <ul class="stars">
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                    <span class="label scriviRecensione">Scrivi una recensione</span>
                </a>
            </div>
            <figure class="itemLogo">
                <div class="img-container-ext">
                    <div class="img-container-int">
                        <a href="https://www.paginegialle.it/teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda" title="Dettagli azienda">
                        <img itemprop="image" alt="Planet Cafe' di Tozzi Iolanda" title="Planet Cafe' di Tozzi Iolanda" data-original="" class="elementImage photo" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" pagespeed_url_hash="1859759222" onload="pagespeed.CriticalImages.checkImageForCriticality(this);">
                        </a>
                    </div>
                </div>
            </figure>
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col">
            <nav class="itemFooter">
                <a class="btn btn-black icn-vetrina shinystat_ssxl" data-pag="vetrina" href="//www.paginegialle.it/teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda">Vetrina</a>
                <a class="btn btn-blank icn-showOnMap btnShowOnMap shinystat_ssxl" data-pag="vedimappa" href="https://www.paginegialle.it/teverola-ce/bar/rivendita-generi-monopolio-n.-2-tozzi-iolanda/mappa" rel="nofollow">  <span>Vedi su mappa</span></a>
            </nav>
        </div>
    </div>
</div>

www.paginegialle.it//ricerca//TABACCO%20PLANET?mr=50 因此,您可能会更轻松地看到HTML。

我编辑并添加一些文本,因为它说了太多代码,不允许我完成编辑,我修复了第一部分,从span更改为h1

1 个答案:

答案 0 :(得分:0)

Xpath与HTML不匹配。相关的片段似乎是:

<div class="hidden-phone-elem">
    <div class="btn btn-yellow btn-show-phone" data-pag="mostra telefono" data-context="listing">
        <span>MOSTRA TELEFONO</span>
    </div>
    <div class="btn btn-hidden-phone">
        <span class="phIco "></span>
        <span class="phone-label">081 5034556</span>
    </div>
</div>

div仅具有类hidden-phone-elem和两个后代span。 Xpath 1.0没有令牌选择器功能,但可以使用字符串函数进行仿真。

  • normalize-space()-用一个空格替换所有空白序列,修剪
  • concat()-连接字符串
  • contains()-查找子字符串

诀窍是将属性归一化为classToMatch otherClass之类,然后查看该属性是否包含classToMatch。 (请注意开头/结尾处的空格。)

$document = new DOMDocument();
$document->loadHTML($html);
$xpath = new DOMXpath($document);

$expression = 'string(
    //div[
      contains(concat(" ", normalize-space(@class), " "), " hidden-phone-elem ")
    ]
    //span[
      contains(concat(" ", normalize-space(@class), " "), " phone-label ")
    ]
)';

var_dump($xpath->evaluate($expression));

输出:

string(11) "081 5034556"