gpt4 book ai didi

php - Xpath 代码在 firepath 中有效,但在 php 脚本中无效

转载 作者:行者123 更新时间:2023-12-02 08:54:55 24 4
gpt4 key购买 nike

        $html = new DOMDocument();
@$html->loadHtmlFile($url);
$xpath = new DOMXPath( $html );
//Query to pull all reviews on the page
$q="//div[starts-with(@id,empReview_)]/h3/meta[1]/@content";
$nodelist = $xpath->query($q);

foreach ($nodelist as $n){
echo $n->nodeValue;
echo"<br><br>";

}

我尝试在以下 XML 上运行的查询是:

<div id="empReview_2055942" class="employerReview" itemscope="" itemtype="http://schema.org/Review">
<h2 class="summary">
<h3 class="review-microdata-heading">
<span class="gdRatingStars"> </span>
Former
<span itemprop="author">IT Engineer Intern in Santa Clarita, CA</span>
<meta content="4" itemprop="reviewRating"/>

使用 Firepath 时它会直接转到元素,但不会通过我在 php 中的查询来回显该值。

任何帮助将不胜感激。

最佳答案

你的xpath查询错误,应该是:

$q="//div[starts-with(@id,empReview_)]/h2/h3/meta[1]/@content";

(您缺少“h2”)。您的代码应该如下所示...

$html = new DOMDocument();

$html->loadHtml('
<div id="empReview_2055942" class="employerReview" itemscope="" itemtype="http://schema.org/Review">
<h2 class="summary">
<h3 class="review-microdata-heading">
<span class="gdRatingStars"> </span>
Former
<span itemprop="author">IT Engineer Intern in Santa Clarita, CA</span>
<meta content="4" itemprop="reviewRating"/>
</h3> <!-- presumably your input has closing tags -->
</h2>
</div>
<div id="empReview_2055947" class="employerReview" itemscope="" itemtype="http://schema.org/Review">
<h2 class="summary">
<h3 class="review-microdata-heading">
<span class="gdRatingStars"> </span>
Former
<span itemprop="author">Some Other Random Thing</span>
<meta content="7" itemprop="reviewRating"/>
</h3>
</h2>
</div>
');

$xpath = new DOMXPath( $html );
//Query to pull all reviews on the page
$q="//div[starts-with(@id,empReview_)]/h2/h3/meta[1]/@content";
$nodelist = $xpath->query($q);

foreach ($nodelist as $n){
echo $n->nodeValue;
echo"<br><br>\n";
}

我得到以下输出:

4<br><br>
7<br><br>

关于php - Xpath 代码在 firepath 中有效,但在 php 脚本中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13282425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com