【问题标题】:Xpath searching for element inside a string rather than the entire thingXpath 搜索字符串中的元素而不是整个元素
【发布时间】:2013-06-07 20:41:51
【问题描述】:

我正在使用简单的 html dom 解析器,我正在尝试从以下字符串中获取公司名称:

<a data-omniture="LIST:COMPANYNAME" href="/b/Elite+Heating+and+Plumbing+Services-Plumbers-Reading-RG46RA-901196207/index.html" itemprop="name"> Elite Heating &amp; Plumbing Services </a>

所以a标签之间的位。

我有以下代码:

<?php include_once('simple_html_dom.php');

$html = file_get_html('http://www.thelink.com/');

foreach($html->find('a') as $element) 
       echo $element->href . '<br>'; echo '</ul>';

?

另外,是否可以搜索 html5 数据的东西,所以 data-whatever:链接中的信息

这会带回所有链接,但显然我不想要所有链接。

【问题讨论】:

    标签: php parsing simple-html-dom domparser


    【解决方案1】:

    是的,你会这样做:

    $name = $html->find('a[itemprop=name]', 0)->text();
    

    顺便说一句,这不是 xpath,而是 css。

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 2019-03-09
      • 2013-05-04
      • 2012-12-06
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多