This repository has been archived by the owner on May 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.php
133 lines (126 loc) · 4.24 KB
/
crawl.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
include("config.php");
include('classes/DomDocumentParser.php');
$alreadyCrawled = array();
$crawling = array();
$alreadyFoundImages = array();
function linkExists($url){
global $con;
$query = $con->prepare("SELECT * FROM sites WHERE url = :url"); //
$query->bindParam(":url", $url);
$query->execute();
return $query->rowCount() != 0;
}
function insertLink($url, $title, $description, $keywords){
global $con;
$query = $con->prepare("INSERT INTO sites(url, title, description, keywords) VALUES (:url, :title, :description, :keywords)");
$query->bindParam(":url", $url);
$query->bindParam(":title", $title);
$query->bindParam(":description", $description);
$query->bindParam(":keywords", $keywords);
return $query->execute();
}
function insertImage($url, $src, $alt, $title){
global $con;
$query = $con->prepare("INSERT INTO images(siteUrl, imageUrl, alt, title) VALUES (:siteUrl, :imageUrl, :alt, :title)");
$query->bindParam(":siteUrl", $url);
$query->bindParam(":imageUrl", $src);
$query->bindParam(":alt", $alt);
$query->bindParam(":title", $title);
return $query->execute();
}
function createLink($src, $url) {
$scheme = parse_url($url)["scheme"]; // http or https
$host = parse_url($url)["host"]; // website.domain
if(substr($src, 0, 2) == "//") {
$src = $scheme . ":" . $src;
}
else if(substr($src, 0, 1) == "/") {
$src = $scheme . "://" . $host . $src;
}
else if(substr($src, 0, 2) == "./") {
$src = $scheme . "://" . $host . dirname(parse_url($url)["path"]) . substr($src, 1);
}
else if(substr($src, 0, 3) == "../") {
$src = $scheme . "://" . $host . "/" . $src;
}
else if(substr($src, 0, 4) != "http" & substr($src, 0, 5) != "https") {
$src = $scheme . "://" . $host . "/" . $src;
}
return $src;
}
function getDetails($url){
global $alreadyFoundImages;
$parser = new DomDocumentParser($url);
$titleArray = $parser->getTitleTags();
if(sizeof($titleArray) == 0 || $titleArray->item(0) == NULL){
return;
}
$title = $titleArray->item(0)->nodeValue; //Get noi dung title url
$title = str_replace("\n", "", $title);
if($title == ""){
return;
}
$description = "";
$keywords = "";
$metaArray = $parser->getMetaTags();
foreach ($metaArray as $meta){
if($meta->getAttribute("name") == "description"){
$description = $meta->getAttribute("content");
}
if($meta->getAttribute("name") == "keywords"){
$keywords = $meta->getAttribute("content");
}
}
$description = str_replace("\n" , "", $description);
$keywords = str_replace("\n", "", $keywords);
if(linkExists($url)){
echo "$url already exists<br>";
}else if(insertLink($url, $title, $description, $keywords)){
echo "Insert success $url to database<br>";
}else{
echo "failed insert $url";
}
$imageArray = $parser->getImages();
foreach ($imageArray as $image){
$src = $image->getAttribute("src");
$alt = $image->getAttribute("alt");
$title = $image->getAttribute("title");
if(!$title && !$alt){
continue;
}
$src = createLink($src, $url);
if(!in_array($src, $alreadyFoundImages)){
$alreadyFoundImages[] = $src;
insertImage($url, $src, $alt, $title);
};
}
}
function followLinks($url) {
global $alreadyCrawled;
global $crawling;
$parser = new DomDocumentParser($url);//phân tích dom;
$link_lists = $parser->getLinks();
foreach ($link_lists as $link){
$href = $link->getAttribute("href");//get only href
if(strpos($href, "#") !== false){ //check url have str #
continue;
}else if(substr($href, 0 ,11) == "javascript:"){//check and remove javascipt
continue;
}
$href = createLink($href, $url);
if(!in_array($href, $alreadyCrawled)){
$alreadyCrawled[] = $href;
$crawling = $href;
getDetails($href);
}
}
array_shift($crawling);
foreach ($crawling as $site) {
followLinks($site);
}
}
if(isset($_POST['url'])){
$start_url = $_POST['url'];
followLinks($start_url);
}