php用xpath解析html的代碼實(shí)例講解
更新時間:2019年02月14日 14:44:34 投稿:laozhang
在本篇文章里小編給大家分享了關(guān)于php用xpath解析html的代碼實(shí)例講解,對此有需要的朋友們可以學(xué)習(xí)下。
實(shí)例1
$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
$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);
}
以上就是相關(guān)的2個實(shí)例內(nèi)容,以及相關(guān)的代碼, 感謝大家對腳本之家的支持。
相關(guān)文章
PHP通過bypass disable functions執(zhí)行系統(tǒng)命令的方法匯總
這篇文章主要介紹了PHP通過bypass disable functions執(zhí)行系統(tǒng)命令的方法匯總,需要的朋友可以參考下2018-05-05
用PHP的socket實(shí)現(xiàn)客戶端到服務(wù)端的通信實(shí)例詳解
本篇文章主要介紹了用PHP的socket實(shí)現(xiàn)客戶端到服務(wù)端的通信實(shí)例詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
PHP加密3DES報錯 Call to undefined function: mcrypt_module_open()
這篇文章主要介紹了PHP加密3DES報錯 Call to undefined function: mcrypt_module_open() 如何解決的相關(guān)資料,需要的朋友可以參考下2016-04-04
Laravel5.1 框架表單驗(yàn)證操作實(shí)例詳解
這篇文章主要介紹了Laravel5.1 框架表單驗(yàn)證操作,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.1框架表單驗(yàn)證的具體實(shí)現(xiàn)步驟、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
OAuth認(rèn)證協(xié)議中的HMACSHA1加密算法(實(shí)例)
下面小編就為大家?guī)硪黄狾Auth認(rèn)證協(xié)議中的HMACSHA1加密算法(實(shí)例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

