Analyzing HTML Page
I've got a question that concerns the analyzing of HTML pages. For example there is an page, www.examp开发者_如何学Cle.com/page.html that contains information in tables that I need, and www.example.com/page2.html has some other information, but in text format. Currently, I'm using an regex (preg_match_all) in which I had to insert a pattern, hand made. Is there a faster/better way to do this. So the full question would be: is there a fast/good way to extract information from an HTML page that doesn't need me to use and edit parts of the source via a regex?
(Other information: I'm using PHP i.c.w. cURL to get the page's content, then I use preg_match_all to extract the data)
Yes! You can load the content of the webpage into a PHP DOMDocument and fetch the data using html classes and IDs just as you would using Javascript.
Here is the documentation http://www.php.net/manual/en/class.domdocument.php
You should start off by using
DOMDocument::loadHTML($html);
Then follow the documentation and it's examples
Use any of the parsers suggested in this post. You should never use regular expressions to parse html.
You can use dom.
精彩评论