Recently want to write a crawler in PHP, you need to parse the HTML, found on SourceForge a project called PHP simple HTML DOM Parser, it can be similar to jquery through the CSS selector to return the specified DOM elements, the function is very powerful.
The first thing to do is introduce simple_html_dom.php this file at the beginning of the program
Copy CodeThe code is as follows:
Include_once (' simple_html_dom.php ');
PHP Simple HTML DOM Parser provides 3 ways to create DOM objects
Copy CodeThe code is as follows:
Create a DOM object from a string
$html = str_get_html ('Hello!');
Create a DOM object from a URL
$html = file_get_html (' http://www.google.com/');
Create a DOM object from a HTML file
$html = file_get_html (' test.htm ');
You can do all kinds of things after you get the DOM object.
Copy CodeThe code is as follows:
Find all anchors, returns a array of element objects
$ret = $html->find (' a ');
Find (N) th anchor, returns element object or null if not found (zero based)
$ret = $html->find (' A ', 0);
Find Lastest Anchor, returns element object or null if not found (zero based)
$ret = $html->find (' A ',-1);
Find all with the id attribute
$ret = $html->find (' div[id] ');
Find all which attribute Id=foo
$ret = $html->find (' div[id=foo] ');
A variety of CSS selectors can be used here, just as handy for DOM operations in jquery. In addition, there are two special attributes to get the contents of text and annotations
Copy CodeThe code is as follows:
Find All text blocks
$es = $html->find (' text ');
Find All Comment ( ) blocks
$es = $html->find (' comment ');
Of course, similar to jquery,php simple HTML DOM parser also supports chained operations, as well as a variety of easy ways to access DOM elements
Copy CodeThe code is as follows:
Example
echo $html->find ("#div1", 0)->children (1)->children (1)->children (2)->id;
Or
echo $html->getelementbyid ("Div1")->childnodes (1)->childnodes (1)->childnodes (2)->getattribute (' Id ');
http://www.bkjia.com/PHPjc/324200.html www.bkjia.com true http://www.bkjia.com/PHPjc/324200.html techarticle recently want to write a crawler in PHP, you need to parse the HTML, found on SourceForge a project called PHP simple HTML DOM Parser, it can be similar to jquery in the way of the CSS selector to return ...