javascript-php Crawl page How to handle can only keep the DOM structure, remove CSS and JS?

Source: Internet
Author: User
Tags xpath
Once the regular rule has been written, the page will be corrected once it has changed.
Is there a better way to extract the DOM of the page first?

Reply content:

Once the regular rule has been written, the page will be corrected once it has changed.
Is there a better way to extract the DOM of the page first?

I think what you need is a PHP DOM module ... Install by default Don't worry ...

Because I don't know what your actual application scenario is ... Let's write you a simple example ...

!--? php/* I heard that you need DOM ... */$doc = new DOMDocument ();/* I wrote a si Mple page ... change it to a curl result ... */$doc--->loadhtml (<< 
   
    Sunyanzi ' s test</t itle> 
   

Hello

World Hey welcomehtml_section);/* Now we should try to get something ... */$h 1Elements = $doc-&G T;getelementsbytagname (' H1 ');/* This line prints "Hello World" ... */foreach ($h 1Elements as $h 1Node) echo $h 1node- >nodevalue, php_eol;/* and this line prints "http://segmentfault.com/" ... */echo $doc->getelementbyid (' Onlylink ' ->getattribute (' href '), php_eol;/* now I'll introduce something advanced ... using XPath ... */$xpath = new Domxpa Th ($doc);/* Also prints "http://segmentfault.com/" ... locate via H1 ... */echo $xpath->evaluate (' String (//h1[tex T () = "Hello World"]/following-sibling::a/@href), Php_eol;

Basically... Wait until you have mastered the XPath ... You'll find DOM is much more flexible than regular ...

PHP's ability to process XML is far beyond your imagination ... Reading a manual is not a bad thing.

You're right about that. I'm going to use XPath now.

  • 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.