Web Crawler-how does PHP use xpath to parse html content?

Source: Internet
Author: User
I have read a lot of related information on the Internet, but PHP uses xpath to parse xml. Is there any function or class library related to PHP that can parse html? Thanks for checking a lot of related information on the Internet, but PHP uses xpath to parse xml. Does PHP have any related functions or class libraries that can parse html? Thank you.

Reply content:

I have read a lot of related information on the Internet, but PHP uses xpath to parse xml. Is there any function or class library related to PHP that can parse html? Thank you.

Use zend-dom directly, which is much more convenient!
Http://framework.zend.com/manual/2.3/en/modules/zend.dom.query.html
Do you need to introduce it?

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

Similar to this

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.