I would like to extract links when you make a google search with a specific keyword.
I know that an API already exists but I want to create my own script.
Currently, I can extract links but my regex is not very good.
# /url?q=http://wikipedia.org/wiki/World_Wide_Web&sa=U&ei=naEHUbDoLITQ0QXWwIGoCw&ved=0CB8QFjAA&usg=AFQjCNEn84GVN2VHpfEVjnMm5FG_p8YjgQ
I don’t know exactly how we can keep only the real url.
So, if you have an idea.
Thanks
$key = "test";
if ($kw = $key){
$pagenum = 0;
$googlefrurl = "http://www.google.com/search?q=" . urlencode($kw) . "&start=$pagenum";
while($pagenum <= 0){
$result= file_get_contents($googlefrurl);
preg_match_all('<a href="(.*?)">si', $result, $matches);
$i = 0;
$n = count($matches[1]);
$pagenum++;
$pagenum2 = $pagenum.'0';
$googlefrurl = "http://www.google.com/search?q=" . urlencode($kw) .
"&start=$pagenum2&safe=off&pwst=1&filter=0";
while($i <= $n){
$u++;
}
}
}
You need a Google API Key to make searchs and then use the custom search REST API
https://developers.google.com/custom-search/v1/using_rest
Which return a JSON with the results and everything you need
For example, the request (quick copy from docs):
Returns