$URLStyle = "http://flash.weather.com.cn/wmaps/xml/%s.xml"
$chinaURL = sprintf($URLStyle, "china")
$chinaStr = file_get_contents($chinaURL)
$chinaObj = simplexml_load_string($chinaStr)
$chinaObjLen = count($chinaObj->city)
echo "chinaObjLen = ".$chinaObjLen."\n"
for ($i=0$i<$chinaObjLen$i++){
$level1 = $chinaObj->city[$i]["pyName"]
$shengjiURL = sprintf($URLStyle, $level1)
$shengjiStr = file_get_contents($shengjiURL)
//echo $shengjiStr
$shengjiObj = simplexml_load_string($shengjiStr)
$shengjiObjLen = count($shengjiObj->city)
// echo $chinaObj->city[$i]["quName"]
// echo " ".$shengjiObjLen."\n"
for ($j=0$j<雀仿肆$shengjiObjLen$j++){
//遍历市一级节点
$level2 = $shengjiObj->city[$j]["pyName"]
$shijiURL = sprintf($URLStyle, $level2)
$shijiStr = file_get_contents($shijiURL)
//echo $shijiStr
$shijiObj = simplexml_load_string($shijiStr)
//直辖市和海南、台湾、钓鱼岛等没有县级节点
if(!$shijiObj){
echo "WARNNING: not exsit next level node. - ".$level1."-".$shijiURL."\n"
echo ' "'.$shengjiObj->city[$j]["cityname"].'" => '
echo $shengjiObj->city[$j]["url"].",\n"
continue
}
$shijiObjLen = count($shijiObj->city)
//echo $shengjiObj->city[$j]["cityname"]." "
//echo $shijiObjLen."\n"
for ($k=0$k<$shijiObjLen$k++){
//遍历县一级节点
顷轿 $xianji_code = $shijiObj->city[$k]["url"]
echo ' "'.$shijiObj->city[$k]["cityname"].'" => '
大虚 echo $shijiObj->city[$k]["url"].",\n"
//echo $xianji_code."\n"
}
}
}
//print_r($chinaObj)
?>
通过XML接口根节点递归获得全国几千个县以上城市cide code的代码
$meta = file_get_contents('http://www.weather.com.cn/weather/101010100.shtml'桐运)//print_r($meta)
preg_match('/<title>(.+?)<闹手\/title>/',$meta,$arr)
print_r($arr)
echo $arr[1]就是液轮嫌你要的
给你简单说下原理,小偷程度野伍原粗仿理:就是比如你要获取中华新闻网站首页的新闻列表,首先你就要分析下该首页的HTML组成格式,知道组成格式后,再写正则表达式,来获取列表和链接地址,这样你就可以获取到想要的内容,把获取到的内容记录到你服务器文本文件,做为数据库使用,岩脊纤然后再用显示程序显示你获取过来的内容。思路就是这样简单,呵呵 ~~欢迎分享,转载请注明来源:内存溢出
评论列表(0条)