Articlebase.com scraping tutorial – part 2, getting search links

In the first part, I have shown how to get links under any category. Now, we will get links when you search articlebase.com with a search term.

Getting HTML

$keyword = ‘beauty’;

$page = intval($page);
$url = “http://www.articlesbase.com/find-articles.php?q=”.strtolower(urlencode($keyword)).”&page=”.urlencode($page);

$html = file_get_contents($url);
if(!$html) return false;

Initialize objects

$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom = new DOMXPath($dom);

Query through DOM

$elements =  $dom->query(“//div[@class=’article_row_middle’]//h3/a”);

Make an empty array to hold links

$links = array();

Find all links and put in the array

if (!is_null($elements)) {
foreach ($elements as $element) {
$links[]=$element->getAttribute(‘href’);
}
} else {
return false;
}

That’s it! Simply and easy! Isn’t it? Again putting them all together.

function get_search_links($keyword,$page=1){
$page = intval($page);
$url = “http://www.articlesbase.com/find-articles.php?q=”.strtolower(urlencode($keyword)).”&page=”.urlencode($page);

$html = abs_init_curl($url);
if(!$html) return false;

$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom = new DOMXPath($dom);
$elements =  $dom->query(“//div[@class=’article_row_middle’]//h3/a”);

$links = array();

if (!is_null($elements)) {
foreach ($elements as $element) {
$links[]=$element->getAttribute(‘href’);
}
} else {
return false;
}
unset($element,$elements);
return $links;
}