Skip to content

Instantly share code, notes, and snippets.

@jamesBan
Created July 24, 2014 07:35
Show Gist options
  • Select an option

  • Save jamesBan/2a868af297be88853200 to your computer and use it in GitHub Desktop.

Select an option

Save jamesBan/2a868af297be88853200 to your computer and use it in GitHub Desktop.
获取具体省份下的街道数据
<?php
set_time_limit(0);
//省份id
$id = isset($_GET['id']) ? intval($_GET['id']) : 33;
//市
$url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/{$id}.html";
$province = pathinfo($url, PATHINFO_FILENAME);
$tmp = curl_get_contents($url);
preg_match_all("/<tr class='citytr'><td><a href='(.+?)'>(.+?)<\/a><\/td><td><a href='(.+?)'>(.+?)<\/a><\/td><\/tr>/m", $tmp, $cities, PREG_SET_ORDER);
unset($tmp);
$data = array();
foreach($cities as $row) {
$city = array(
'id' => $row[2],
'link' => $row[3],
'name' => iconv('gbk', 'utf-8', $row[4]),
);
//区
$link = $row[3];
$url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/$link";
$result = curl_get_contents($url);
preg_match_all("/<tr class='countytr'><td><a href='(.+?)'>(.+?)<\/a><\/td><td><a href='(.+?)'>(.+?)<\/a><\/td><\/tr>/m", $result, $ques, PREG_SET_ORDER);
foreach($ques as $row){
$qu = array(
'id' => $row[2],
'link' => $row[3],
'name' => iconv('gbk', 'utf-8', $row[4]),
);
//街道
$link = $row[3];
$url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/$province/{$link}";
$result = curl_get_contents($url);
preg_match_all("/<tr class='towntr'><td><a href='(.+?)'>(.+?)<\/a><\/td><td><a href='(.+?)'>(.+?)<\/a><\/td><\/tr>/m", $result, $street, PREG_SET_ORDER);
$tmp_street = array();
foreach($street as $row){
$tmp_street[] = array(
'id' => $row[2],
'link' => $row[3],
'name' => iconv('gbk', 'utf-8', $row[4]),
);
}
$qu['list'] = $tmp_street;
unset($tmp_street);
$city['list'][] = $qu;
}
$data[] = $city;
}
$data = serialize($data);
$key = 'cache_'.$id;
file_put_contents('cache/'. $key, $data);
/**
* [httpGet description]
* @param [type] $url [description]
* @return [type] [description]
*/
function httpGet($url)
{
global $id;
if(!is_dir("html/{$id}")){
mkdir("html/{$id}");
}
$fileName = pathinfo($url, PATHINFO_BASENAME);
$fileName = "html/{$id}/$fileName";
if(file_exists($fileName) && filesize($fileName)){
return file_get_contents($fileName);
}
$context = stream_context_create(
array(
'http' => array('timeout' => 5)
)
);
$result = file_get_contents($url, false, $context);
file_put_contents($fileName, $result);
return $result;
}
function curl_get_contents($url)
{
global $id;
if(!is_dir("html/{$id}")){
mkdir("html/{$id}");
}
$fileName = pathinfo($url, PATHINFO_BASENAME);
$fileName = "html/{$id}/$fileName";
if(file_exists($fileName) && filesize($fileName)){
return file_get_contents($fileName);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址
//curl_setopt($ch,CURLOPT_HEADER,1); //是否显示头部信息
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //设置超时
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'); //用户访问代理 User-Agent
curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com'); //设置 referer
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟踪301
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回结果
$r = curl_exec($ch);
curl_close($ch);
file_put_contents($fileName, $r);
return $r;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment