亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

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);

 

// create document object model

$dom = new DOMDocument();

// load html into document object model

@$dom->loadHTML($html);

// create domxpath instance

$xPath = new DOMXPath($dom);

// get all elements with a particular id and then loop through and print the href attribute

$elements = $xPath->query('//*[@id="lg"]/img/@src');

foreach ($elements as $e) {

  echo ($e->nodeValue);

}


標(biāo)簽: PHP
分享:
評(píng)論:
你還沒(méi)有登錄,請(qǐng)先