On the internet to see a lot of relevant information, but all PHP with XPath parsing XML, do you have any related functions or libraries to parse HTML? Thank you
Reply content:
On the internet to see a lot of relevant information, but all PHP with XPath parsing XML, do you have any related functions or libraries to parse HTML? Thank you
Directly with Zend-dom Bar, more convenient!
Http://framework.zend.com/manual/2.3/en/modules/zend.dom.query.html
Introduction no need to teach, right?
$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);}
Almost like this.