Web crawler-How does PHP use XPath to parse HTML content?

Source: Internet
Author: User
Tags xpath
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.

  • Related Article

    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.