php利用xpath解析html的代碼示例
來(lái)源:不言
發(fā)布時(shí)間:2019-03-23 16:14:14
閱讀量:1044
本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于php利用xpath解析html的代碼示例,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。
實(shí)例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/>" ;
}
|
實(shí)例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);
}
|