使用SpanId从PHP获取文本:PHP

时间:2015-04-19 06:12:40

标签: php xpath

<?php
$doc = new DOMDocument;
$doc->load("www.xyz.com/ABC");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/div[@id='myspanId']");
?>

我想通过网页“www.xyz.com/ABC”获取“myspanId”的价值。
但它显示错误。

还尝试了:$doc->loadHTML("www.xyz.com/ABC");

1 个答案:

答案 0 :(得分:-1)

你可能不需要卷曲,但这就是我接近它的方式。

$curl=curl_init( 'http://www.example.com/ABC' );
/* Set other curl options */
$response=curl_exec( $curl );
curl_close( $curl );


libxml_use_internal_errors( true );
$dom = new DOMDocument('1.0','utf-8');
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->preserveWhiteSpace=true;
$dom->strictErrorChecking=false;
$dom->substituteEntities=false;
$dom->recover=true;
$dom->formatOutput=false;       
$dom->loadHTML( mb_convert_encoding( $response, 'utf-8' ) );    
$parse_errs=serialize( libxml_get_last_error() );
libxml_clear_errors();


/* there was a typo here - should be DOMXPath! */
$xpath=new DOMXPath( $dom );
$elements = $xpath->query("*/div[@id='myspanId']");