Created
November 10, 2010 08:05
-
-
Save Ozerich/670528 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| set_time_limit(111111); | |
| $db_host = "localhost"; | |
| $db_user = "root"; | |
| $db_password = ""; | |
| $db_dbname = "spr"; | |
| $db_tablename = "data"; | |
| $log_file = "log.txt"; | |
| $f = fopen($log_file, "w+"); | |
| mysql_connect($db_host, $db_user, $db_password) or die("Can't connect to mysql"); | |
| mysql_select_db($db_dbname) or die("Can't connect to database $db_dbname"); | |
| @mysql_query("DROP TABLE $db_tablename"); | |
| mysql_query("CREATE TABLE $db_tablename( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| section VARCHAR(100), | |
| subsection VARCHAR(100), | |
| name VARCHAR(500), | |
| address VARCHAR(500), | |
| timetable VARCHAR(500), | |
| site VARCHAR(100)) | |
| DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"); | |
| function getPage($url) | |
| { | |
| $ch = curl_init (); | |
| curl_setopt ($ch , CURLOPT_URL , $url); | |
| curl_setopt ($ch , CURLOPT_USERAGENT , "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.7.12) Gecko/20050919 Firefox/1.0.7"); | |
| curl_setopt ($ch , CURLOPT_RETURNTRANSFER , 1 ); | |
| curl_setopt ($ch,CURLOPT_HTTPHEADER,array ("Content-Type: text/xml; charset=windows-1251","Expect: 100-continue")); | |
| $content = curl_exec($ch); | |
| $content = iconv("cp1251", "UTF-8", $content); | |
| curl_close($ch); | |
| return $content; | |
| } | |
| function addItemToBase($section_name, $subsection_name, $name, $address, $timetable, $site) | |
| { | |
| global $db_tablename; | |
| $query = "INSERT INTO ".$db_tablename. " SET section='".mysql_escape_string($section_name)."', | |
| subsection='".mysql_escape_string($subsection_name)."', | |
| name='".mysql_escape_string($name)."', | |
| address='".mysql_escape_string($address)."', | |
| timetable='".mysql_escape_string($timetable)."', | |
| site='".mysql_escape_string($site)."'"; | |
| mysql_query($query) or die(mysql_error()); | |
| } | |
| function parseItem($url, $section_name, $subsection_name) | |
| { | |
| $url = "http://www.spr.ru/".$url.".html"; | |
| $name = $address = $timetable = $site = ""; | |
| $text = getPage($url); | |
| preg_match("#<H1 class='main' style='font-size:24px;'>(.+?)</H1>#sui", $text, $name); | |
| $name = str_replace(" ", "", htmlspecialchars_decode(strip_tags($name[1]))); | |
| preg_match("#<font class='view-orange'>Адрес</font> <font class='view-font'>(.+?) <noindex>#sui", $text, $address); | |
| $address = str_replace(" ", "", htmlspecialchars_decode(strip_tags($address[1]))); | |
| preg_match("#<font class='view-orange'>Часы работы</font><font class='font'> </font><font class='view-font'>(.+?)</font>#sui", $text, $timetable); | |
| $timetable = $timetable[1]; | |
| preg_match("#<font class='view-orange'>Сайт</font><font class='font'> </font><font class='view-font'>(.+?)</font>#sui", $text, $site); | |
| $site = $site[1]; | |
| addItemToBase($section_name, $subsection_name, $name, $address, $timetable, $site); | |
| } | |
| function parseCity($url) | |
| { | |
| global $f; | |
| $text = getPage($url); | |
| preg_match_all("#<noindex><a rel='nofollow' href='(.+?)' title='(.+?)' style='font-size:14px;'>(.+?)</a></noindex><br>#sui", $text, $sections, PREG_SET_ORDER); | |
| foreach($sections as $section) | |
| { | |
| $section_url = $section[1]; | |
| $section_name = $section[3]; | |
| $section_text = getPage($section_url); | |
| preg_match_all("#<font style='font-size:16px; color:222222;'>(.+?)<br><br>#sui", $section_text, $subsections_content, PREG_SET_ORDER); | |
| foreach($subsections_content as $subsection_item) | |
| { | |
| preg_match_all("#<a href='(.+?)' title='(.+?)' style='line-height:17px; font-size:14px;'>(.+?)</a>#sui", $subsection_item[1], $subsections, PREG_SET_ORDER); | |
| foreach($subsections as $subsection) | |
| { | |
| $subsection_url = $subsection[1]; | |
| $subsection_name = $subsection[3]; | |
| $subsection_text = getPage($subsection_url); | |
| preg_match_all('#firms.push\(\["(.+?)","(.+?)","(.+?)",(.+?)\]\);#sui', $subsection_text, $items, PREG_SET_ORDER); | |
| $count += count($items); | |
| foreach($items as $item) | |
| parseItem($item[3], $section_name, $subsection_name); | |
| } | |
| } | |
| } | |
| fwrite($f, $url."---->".$count); | |
| } | |
| $text = getPage("http://www.spr.ru/"); | |
| preg_match('#<ul class="Control">(.+?)</ul>#sui', $text, $regions_content); | |
| preg_match_all('#<li><a href="(.+?)" rel=\'nofollow\'>(.+?)</a></li>#sui', $regions_content[0], $regions, PREG_SET_ORDER); | |
| foreach($regions as $region) | |
| { | |
| $url = $region[1]; | |
| $url = "http://www.spr-cfo.ru/"; | |
| $text = getPage($url); | |
| preg_match_all("#<td valign='top' width='95%'>(.+?)</td>#sui", $text, $temp, PREG_SET_ORDER); | |
| foreach($temp as $letter) | |
| { | |
| preg_match_all("#<a href='(.+?)' title='(.+?)'>(.+?)</a><font style='color:676d74;'>(.+?)</font>#sui", $letter[1], $elems, PREG_SET_ORDER); | |
| foreach($elems as $elem) | |
| $sections[] = $elem[1]; | |
| } | |
| preg_match_all("#<font class='list'>(.+?)</font>#sui", $text, $sections2, PREG_SET_ORDER); | |
| foreach($sections2 as $section) | |
| { | |
| preg_match_all("#<a href='(.+?)' title='(.+?)'>(.+?)</a><br>#sui", $section[1], $items, PREG_SET_ORDER); | |
| foreach($items as $item) | |
| $sections[] = $item[1]; | |
| } | |
| foreach($sections as $section) | |
| parseCity($section); | |
| exit(); | |
| } | |
| fclose($f); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment