-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.php
48 lines (35 loc) · 1.18 KB
/
scraper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
#file still a work in progress
/**
* Created by IntelliJ IDEA.
* User: Andre
* Date: 2017-09-28
* Time: 2:48 PM
*/
$q = $_REQUEST['search'];
$whatIWant = substr($q, strpos($q, ","));
$query = $whatIWant . '+earthquake+news';
//https://www.google.com/search?newwindow=1&safe=active&dcr=0&q=mexico+earthquake+news&oq=mexico+earthquake+news
$html = file_get_contents('https://www.google.com/search?q='.$query.'&oq='.$query); //get the html returned from the following url
$doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$doc->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$xpath = new DOMXPath($doc);
//get all the h3's with a class
$row = $xpath->query('//h3[@class="r"]');
if($row->length > 0){
foreach($row as $entry){
$val = $entry->nodeValue;
$nodes = $xpath->query("a/attribute::href", $entry);
foreach( $nodes as $node ) {
echo "https://www.google.co.za/".$node->nodeValue."</br>";
}
}
} else {
echo 'nothing';
}
} else {
die('nothing');
}