-
Notifications
You must be signed in to change notification settings - Fork 1
/
Crawler.php
117 lines (109 loc) · 2.86 KB
/
Crawler.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
Class Crawler
{
var $curl;
function __construct()
{
$this->curl= curl_init();
}
function getContent($url)
{
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1);
$content=curl_exec ($this->curl);
return $content;
}
function hasProtocol($url)
{
return strpos($url,"//");
}
function getDomain($url)
{
return substr($url,0,strrpos($url,"/"));
}
//Convert the link as It should be
function convertLink($domain,$url,$link)
{
if($this->hasProtocol($link))
{
return $link;
}
elseif (($link=='#')||($link=="/"))
{
return $url;
}
//else if((strpos($link,'/'))==0)
else if(substr($link,0,1)=="/")
{
return $domain.$link;
}
else
{
return $domain."/".$link;
}
}
function crawlLinks($url)
{
$content=$this->getContent($url);//get the whole page
$domain=$this->getDomain($url);
$dom = new DOMDocument();
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("//a");//get all a tags
$p = $xpath->evaluate("//p");
for ($i = 0; $i < $hrefs->length; $i++)
{
$href = $hrefs->item($i);//select an a tag
$links['link'][$i]=$this->convertLink($domain,$url,$href->getAttribute('href'));
$links['text'][$i]=$href->nodeValue;
$p1 = $p->item($i);
$links['ptag'][$i]=$p1->nodeValue;
//echo $links['ptag'][$i];
}
return $links;
}
//function for crawl image
function crawlImage($url)
{
$content=$this->getContent($url);
$domain=$this->getDomain($url);
$dom = new DOMDocument();
@$dom->loadHTML($content);
$xdoc = new DOMXPath($dom);
//Read the images that is between <a> tag
$atags = $xdoc ->evaluate("//a"); //Read all a tags
$index=0;
for ($i = 0; $i < $atags->length; $i++)
{
$atag = $atags->item($i); //select an a tag
$imagetags=$atag->getElementsByTagName("img");//get img tag
$imagetag=$imagetags->item(0);
if(sizeof($imagetag)>0)//if img tag exists
{
$imagelinked['src'][$index]=$imagetag->getAttribute('src');//save image src
$imagelinked['link'][$index]=$atag->getAttribute('href');//save image link
$index=$index+1;
}
}
//Read all image
//Betweem <img> tag
$imagetags = $xdoc ->evaluate("//img"); //Read all img tags
$index=0;
$indexlinked=0;
for ($i = 0; $i < $imagetags->length; $i++)
{
$imagetag = $imagetags->item($i);
$imagesrc=$imagetag->getAttribute('src');
$image['link'][$index]=null;
if($imagesrc==$imagelinked['src'][$indexlinked])//check wheather this image has link
{
$image['link'][$index]=$this->convertLink($domain,$url,$imagelinked['link'][$indexlinked]);
$indexlinked=$indexlinked+1;
}
$image['src'][$index]=$this->convertLink($domain,$url,$imagesrc);
$index=$index+1;
}
return $image;
}
}
?>