Web scraping is the only way to get data from websites when websites don’t provide API to access it’s data. Web scraping involves the following steps to get data:
- Send a request to the web page
- Parse/Extract data that you want to scrape from the website.
- Store data for final output (excel, CSV,mysql database, etc).
Web scraping can be implemented in any language such as PHP, Java, .Net, Python, and other languages, that allow us to make web requests and get web page content (HTML text) into variables. In this article, I will show you how to use Simple HTML DOM PHP library to do web scraping using PHP.
PHP Simple HTML DOM Parser
Simple HTML DOM is a PHP library to parse data from webpages, in short, you can use this library to do web scraping using PHP and even store data in the MySQL database. Simple HTML DOM has the following features:
- The parser library is written in PHP 5+
- It requires PHP 5+ to run
- Parser supports invalid HTML parsing.
- It allows you to select HTML tags like JQuery way.
- Supports Xpath and CSS path-based web extraction
- Provides both the way – Object oriented way and procedure way to write code
Scrape All Links
<?php
include "simple_html_dom.php";
//create object
$html=new simple_html_dom();
//load specific URL
$html->load_file("http://www.google.com");
// This will Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
?>
Scrape images
<?php
include "simple_html_dom.php";
//create object
$html=new simple_html_dom();
//load specific url
$html->load_file("http://www.google.com");
// This will Find all links
foreach($html->find('img') as $element)
echo $element->src . '<br>';
?>
This is just a little idea of how you can do web scraping using PHP. Keep in mind that Xpath can make your job much simpler and faster. You can find all methods available on the SimpleHTMLDom documentation page.
