-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.php
More file actions
134 lines (116 loc) · 3.99 KB
/
crawler.php
File metadata and controls
134 lines (116 loc) · 3.99 KB
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
134
<?php
class Crawler{
public $content;
private $__conn = '';
public $_element_parent = '';
public $_title = '';
public $_date = '';
public $_content = '';
function save($url){
$this->get_data($url,$this->_element_parent,$this->_title,$this->_date,$this->_content);
//var_dump($this->content);
$this->connect();
$this->insert('news',$this->content);
$this->dis_connect();
}
function get_data($url,$element_parent,$title,$date,$content) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$document = new \DomDocument('1.0', 'UTF-8');
libxml_use_internal_errors(true);
$document->loadHTML($result);
libxml_use_internal_errors(false);
$xpath = new DomXPath($document);
$items = $xpath->query($element_parent);
$data_array = array();
foreach ($items as $item) {
$data['title'] = trim($xpath->query($title,$item)->item(0)->nodeValue);
$data['url'] = $this->changeTitle($data['title']);
$ngaythang = trim($xpath->query($date,$item)->item(0)->nodeValue);
preg_match('/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/',$ngaythang,$view);
$cvtime = implode('',$view);
$str_date = DateTime::createFromFormat('d/m/Y',$cvtime);
$date_insert = $str_date->format('Y-m-d');
$data['date'] = $date_insert;
$data['content'] = trim($xpath->query($content,$item)->item(0)->nodeValue);
$data['description'] = substr($data['content'], 0,200);
$data_array[] =$data;
}
$this->content = $data_array;
}
//Hàm kết nối
function connect(){
try {
$this->__conn = new PDO('mysql:host=localhost;dbname=php_example;charset=utf8','root','');
//echo 'Kết nối thành công';
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
//Hàm ngắt kết nối
function dis_connect(){
$this->__conn = null;
}
function insert($table,$data){
//Lưu trữ danh sách field
$field_list = '';
//Lưu trữ danh sách giá trị tương ứng với field
$value_list = '';
foreach ($data[0] as $key => $value) {
//var_dump($value);
$field_list .= ','.$key;
$value_list .= ','.$this->__conn->quote($value);
}
$field_key = trim($field_list, ',');
$field_val = trim($value_list, ',');
//var_dump($field_val);
//Vì sau vòng lặp các biến $field_list và $value_list sẽ thừa 1 dấu ,nên ra sẽ dùng hàm
//trim để xóa đi
$sql = "INSERT INTO $table($field_key) VALUES ($field_val)";
//var_dump($sql);
$stmt = $this->__conn->exec($sql);
return $stmt;
}
function stripUnicode($str){
if(!$str) return false;
$unicode = array(
'a' =>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
'A' =>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
'd' =>'đ',
'D' =>'Đ',
'e' =>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
'E' =>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
'i' =>'í|ì|ỉ|ĩ|ị',
'I' =>'Í|Ì|Ỉ|Ĩ|Ị',
'o' =>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
'O' =>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
'u' =>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
'U' =>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
'y' =>'ý|ỳ|ỷ|ỹ|ỵ',
'Y' =>'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
);
foreach ($unicode as $khongdau => $codau) {
$arr=explode("|",$codau);
$str = str_replace($arr,$khongdau,$str);
}
return $str;
}
function changeTitle($str){
$str=trim($str);
if ($str=="") return "";
$str =str_replace('"','',$str);
$str =str_replace("'",'',$str);
$str = $this->stripUnicode($str);
$str = mb_convert_case($str,MB_CASE_LOWER,'utf-8');
// MB_CASE_UPPER / MB_CASE_TITLE / MB_CASE_LOWER
$str = str_replace(' ','-',$str);
return $str;
}
}
?>