php利用xpath解析html的代码示例
来源:不言
发布时间:2019-03-23 16:14:14
阅读量:1208
本篇文章给大家带来的内容是关于php利用xpath解析html的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
实例1
1 2 3 4 5 6 7 | $xml = simplexml_load_file( 'https://forums.eveonline.com' );
$names = $xml ->xpath( "html/body/p/p/form/p/p/p/p/p[*]/p/p/table//tr/td[@class='topicViews']" );
foreach ( $names as $name )
{
echo $name . "<br/>" ;
}
|
实例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $url = 'http://www.baidu.com' ;
$ch = curl_init();
curl_setopt( $ch , CURLOPT_FILE, fopen ( 'php://stdout' , 'w' ));
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt( $ch , CURLOPT_URL, $url );
$html = curl_exec( $ch );
curl_close( $ch );
$dom = new DOMDocument();
@ $dom ->loadHTML( $html );
$xPath = new DOMXPath( $dom );
$elements = $xPath ->query( '//*[@id="lg"]/img/@src' );
foreach ( $elements as $e ) {
echo ( $e ->nodeValue);
}
|