脚本抓取邮编

来源:互联网 发布:医疗器械进销存软件 编辑:程序博客网 时间:2024/04/26 02:45

$sql = "select area_id,area_name from areas where post_code = '' and area_name <> '市辖区' and area_name <> '县' and parent_id != 0 and area_name <> '省直辖县级行政区划' and area_name <> '自治区直辖县级行政区划'";


$result = $mysqli->query($sql,$con);
while($rows = $result->fetch_all()){
foreach($rows as $row){
$info[$row[0]] = $row[1];
}
}
if(!empty($info)){
foreach($info as $key=>$value){
$name = iconv("utf-8","gbk",$value);
$name = urlencode($name);
$url = "http://opendata.baidu.com/post/s?wd=".$name."&p=mini&rn=20";
$html = file_get_contents($url);
preg_match_all('@<td>(.*?)</td>@is', $html, $matches);
if(!empty($matches)){
$post_code = $matches[1][0];
$sql = "update areas set `post_code` = ".$post_code." where area_id = ".$key." limit 1;";
$mysqli->query($sql);
}
}
}
0 0
原创粉丝点击