vk.com парсер

 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
<?php
set_time_limit(1800);
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1]+$mtime[0];
require 'parser.php';
require '../kernel/system.php';
define(ROOT, 'http://vkontakte.ru/');
define(REG, ROOT . 'reg0');
define(SCHOOLS, ROOT . 'help.php?act=agetselect&list_id=school1&val=');
define(UNIVERS, ROOT . 'help.php?act=agetselect&list_id=university&val=');
define(FACULTY, ROOT . 'help.php?act=agetselect&list_id=faculty&val=');
$html = file_get_html( REG );
$options = $html->find('#uni_city option');
$cities = array();
$schools = array();
$univers = array();
$faculty = array();
// Init
foreach($options as &$opt)
if( intval($opt->value) > 0 )
$cities[] = array(
'city_ID' => $opt->value,
'cityName' => $opt->innertext
);
// GRAB
function grabData($url, $id, $iid, $pid, $name){
$html = file_get_html( $url . $id );
$options = $html->find('option');
$ret = array();
foreach($options as &$opt){
if( intval($opt->value) > 0 )
$ret[] = array(
$iid => $opt->value,
$pid => $id,
$name => iconv('UTF-8', 'cp1251', $opt->innertext)
);
}
return $ret;
}
foreach($cities as &$city){
$schools = array_merge(&$schools, grabData(SCHOOLS, $city['city_ID'], 'school_college_ID', 'city_ID', 'school_collegeName'));
$univers = array_merge(&$univers, grabData(UNIVERS, $city['city_ID'], 'institute_ID', 'city_ID', 'instituteName'));
}
foreach($univers as &$univer)
$faculty = array_merge(&$faculty, grabData(FACULTY, $univer['institute_ID'], 'faculty_ID', 'institute_ID', 'facultyName'));
function export(&$data, $table){
echo '<h1>' . $table . '</h1>';
foreach($data as &$d){
$fields = "";
$values = "";
$query = "Insert INTO " . $table;
foreach($d as $f => &$v){
$fields .= $f.',';
$values .= "'".$v."',";
}
$fields = preg_replace("/,$/","", $fields);
$values = preg_replace("/,$/","", $values);
$query .= "(".$fields.") values (".$values.");";
//echo '<p>' . $query . '</p>';
//file_put_contents('./out.sql', file_get_contents('./out.sql') . $query . "\n\n");
mysql_query($query);
}
}
export($cities, 'city');
export($faculty, 'faculty');
export($univers, 'university');
export($schools, 'schools_colleges');
$mtime2 = microtime();
$mtime2 = explode(' ', $mtime2);
$mtime2 = $mtime2[1]+$mtime2[0];
echo $mtime2 - $mtime . ' MS';
?>