![]() |
|
PHP HTML DOM Parser - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: PHP HTML DOM Parser (/Thread-PHP-HTML-DOM-Parser) |
PHP HTML DOM Parser - hackarchives - 01-26-2013 The HTML DOM defines a standard way for accessing and manipulating HTML documents. What we are going to do is to read a document using PHP Simple HTML DOM Parser and then manipulate it according to our needs DOWNLOAD Download The script form here and extract it Now we need to create a PHP file to start using the script. Firstly, include simple_html_dom.php in the php file PHP Code: <?php
include('simple_html_dom.php');
?>Now we need to access a page and manipulate it according to our needs. we will first get the contents of the page. PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
?>Now your whole page is stored in $html.Yo can test it by echoing it To access elements using this script we use foreach loop. 1.Accessing links PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('a') as $element)
echo $element->href . ' - '.$element->innerhtml;
?>As you may have noticed in the script above we used find() function to find an element and so is its use.You may use other tags like img,h1,h2,title etc. on it to find them in a document. $element->href - This gets the url the link is pointing to.It basically got the href attribute. Note:You may replace href by any attribute you wish to get $element->innerhtml - This gets the text between <a> and </a> tags 2.Accessing Images PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('img') as $element)
echo $element->src;
?>3.Getting elements with a particular id or class PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('#xyz') as $element)
echo $element;
?>4.Getting Div with particular class PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('div.xyz') as $element)
echo $element;
?>5.Getting all elements that has a certain attribute PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('*[xyz]') as $element)
echo $element;
?>find('*[xyz]') does the task. * operator makes the script to gets all the element and then [xyz] finds the elements with xyz attribute . 6.Getting some elements with some attribute PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('abc[xyz]') as $element)
echo $element;
?>find('abc[xyz]') - in this you define the element you want in place of abc and xyz is the attribute you want to access 7.Getting some element with some attribute with some value PHP Code: <?php
include('simple_html_dom.php');
$html = file_get_html('http://yoururlhere');
foreach($html->find('abc[xyz=pqr]') as $element)
echo $element;
?>Similarly you may see this table for usage [from official website] ![]() This is all for this tutorial.If you need anything more to be explained just tell me.. All reviews welcomed(although positive will be more appreciated) Note: Have been posted by me on HF before.SO don't tell me that it i should have mentioned the source RE: PHP HTML DOM Parser - The Real Slim Shady - 01-26-2013 sorry im on my phone so i cant look at the php file... but what is stopping it from returning every single 'a' in the webpage. not just the html tags? looks like a very useful script though. ill download it later as im sure i can put it to good use in the future RE: PHP HTML DOM Parser - hackarchives - 01-26-2013 It's not my script.It's by someone else.I just wrote a tutorial on it's usage. The reason it does not select all 'a' is because of regex RE: PHP HTML DOM Parser - bluedog.tar.gz - 01-26-2013 Well detailed tutorial, thanks RE: PHP HTML DOM Parser - The Real Slim Shady - 01-26-2013 (01-26-2013, 11:46 AM)hackarchives Wrote: It's not my script.It's by someone else.I just wrote a tutorial on it's usage. Ah okay. Still useful. Just got back home so ill take a closer look at the script shortly. Thanks for the share. RE: PHP HTML DOM Parser - hackarchives - 01-26-2013 Glad you liked it guys.I have scraped many sites with the use of this DOM Parser RE: PHP HTML DOM Parser - The Alchemist - 07-10-2013 I'm thinking of using this on a site that contains a collection of SMSes to get myself a huge collection of SMSes for building a Windows Phone app. RE: PHP HTML DOM Parser - The Alchemist - 07-10-2013 I'm thinking of using this on a site that contains a collection of SMSes to get myself a huge collection of SMSes for building a Windows Phone app. RE: PHP HTML DOM Parser - hackarchives - 07-10-2013 (07-10-2013, 06:04 AM)The Alchemist Wrote: I'm thinking of using this on a site that contains a collection of SMSes to get myself a huge collection of SMSes for building a Windows Phone app. Yes it can be used.If you need help , you can ask me XD RE: PHP HTML DOM Parser - VipVince - 07-10-2013 Just like its friend file_get_contents(), useful when right clicking and other procedures to stop viewing source are blocked. |