首先了解一串代碼
<?php
$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);
} ?> 在PHP中是可以直接運作輸出的,簡單的學習一下xpath文法規則即可看懂'//*[@id="lg"]/img/@src'這串的含義,不過這并不是重要的,谷歌浏覽器自帶xpath,文法規則可以自動生成,另外還可以裝一個XPath Helper插件,具體可以看截圖
1.打開百度按F12,如圖顯示的這樣。
2.點選左側的箭頭圖示。
轉載于:https://www.cnblogs.com/xinyu2017/p/7424796.html