简体   繁体   English

如何在php中获取Span标签文本?

[英]How to get Span tag text in php?

I am scraping a website in PHP. 我正在用PHP抓取一个网站。 I have scraped all the required data but I am unable to scrape span tag text. 我已经抓取了所有必需的数据,但是无法抓取跨度标签文本。

Expected Output: Apr 20,2017 预期产量: Apr 20,2017

<span title="" data-toggle="tooltip" data-original-title="Posted On">
                                        <i class="calendar rz-calendar"></i>Apr 20, 2017
                                    </span>
$html = file_get_contents($url);
libxml_use_internal_errors( true);
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXpath( $doc);

$node = $xpath->query( '//span[@data-original-title="Posted ON"]');

Problem in your code is //span[@data-original-title="Posted ON"] ON is not in capitals it is like this On 您代码中的问题是//span[@data-original-title="Posted ON"] ON不是大写,就像这样On

Try this code snippet here 在此处尝试此代码段

<?php
ini_set('display_errors', 1);


$doc = new DOMDocument;
$doc->loadHTML('<span title="" data-toggle="tooltip" data-original-title="Posted On">
                                        <i class="calendar rz-calendar"></i>Apr 20, 2017
                                    </span>');
$xpath = new DOMXpath( $doc);
$nodeList = $xpath->query( '//span[@data-original-title="Posted On"]');
foreach($nodeList as $node)
{
    echo trim($node->textContent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM