一、分析数据的所在位置
在爬取数据的过程中,第一件事往往就是分析数据的所在位置。我们可以通过在腾讯天气官网按下F12,在NetWork中的一些文件里找到我们所需的数据。就像这样: 在这个URL中,里面的数组包含了我们所需要的数据,即天气,晴雨天等等。那么我们就复制这个URL,接下来的工作也会围绕这个URL展开。URL: https://wis.qq.com/weather/common?source=xw&refer=h5&weather_type=observe|rise&province=%E5%8C%97%E4%BA%AC%E5%B8%82&city=%E5%8C%97%E4%BA%AC%E5%B8%82&county=%E6%B5%B7%E6%B7%80%E5%8C%BA&callback=reqwest_1627223795947
二、获得URL上的信息
通过一个一个函数file_get_contents()就可以获得该URL上的信息。并且通过var_dump()调试输出。
代码如下(示例):
$URL = 'https://wis.qq.com/weather/common?source=xw&refer=h5&weather_type=observe|rise&province=%E5%8C%97%E4%BA%AC%E5%B8%82&city=%E5%8C%97%E4%BA%AC%E5%B8%82&county=%E6%B5%B7%E6%B7%80%E5%8C%BA&callback=reqwest_1627115817985';
$msg = file_get_contents($URL);
var_dump($msg);
三、分析所得的信息
调试输出文本如下():
string(1164) "reqwest_1627115817985({"data":{"observe":{"degree":"29","humidity":"75"
,"precipitation":"0","pressure":
"996","update_time":"202107252145",
"weather":"阴","weather_code":"02",
"weather_short":"阴","wind_direction":"4",
"wind_power":"1"},"rise":{"0":
{"sunrise":"05:06","sunset":"19:35","time":"20210725"},"1":
{"sunrise":"05:07","sunset":"19:34","time":"20210726"},"10":
{"sunrise":"05:16","sunset":"19:25","time":"20210804"},"11":
{"sunrise":"05:17","sunset":"19:24","time":"20210805"},"12":
{"sunrise":"05:17","sunset":"19:23","time":"20210806"},"13":
{"sunrise":"05:18","sunset":"19:22","time":"20210807"},"14":
{"sunrise":"05:19","sunset":"19:21","time":"20210808"},"2":
{"sunrise":"05:08","sunset":"19:33","time":"20210727"},"3":
{"sunrise":"05:09","sunset":"19:33","time":"20210728"},"4":
{"sunrise":"05:10","sunset":"19:32","time":"20210729"},"5":
{"sunrise":"05:11","sunset":"19:31","time":"20210730"},"6":
{"sunrise":"05:12","sunset":"19:30","time":"20210731"},"7":
{"sunrise":"05:13","sunset":"19:29","time":"20210801"},"8":
{"sunrise":"05:14","sunset":"19:27","time":"20210802"},"9":
{"sunrise":"05:15","sunset":"19:26","time":"20210803"}}},
"message":"OK","status":200})"
这里的数据是json格式,所以我们只需要使用正则表达式匹配出相应字符串,再转换成数组即可获得我们需要的温度、天气信息。
四、正则表达式
从第一个大括号开始,到最后一个大括号结束,都是我们要的json。那么就很简单了。
$preg = '/\((.+)\)/';
preg_match($preg,$msg,$matches);
如此一来,就可以匹配出我们需要的数据。再用json_decode()解码即可。
$arr = json_decode($matches[1],true);
此时我们获得的$arr数组就是最上面的那行数组,即(reqwest…),最后我们通过关联数组的查找方式即可轻松访问到我们需要的数据。
echo ($arr['data']['observe']['degree']);
echo ($arr['data']['observe']['weather']);
五、代码
$URL = 'https://wis.qq.com/weather/common?source=xw&refer=h5&weather_type=observe|rise&province=%E5%8C%97%E4%BA%AC%E5%B8%82&city=%E5%8C%97%E4%BA%AC%E5%B8%82&county=%E6%B5%B7%E6%B7%80%E5%8C%BA&callback=reqwest_1627115817985';
$msg = file_get_contents($URL);
var_dump($msg);
$preg = '/\((.+)\)/';
preg_match($preg,$msg,$matches);
$arr = json_decode($matches[1],true);
echo ($arr['data']['observe']['degree']);
echo ($arr['data']['observe']['weather']);
六、不足
由于一文没有找到各个城市的字符串代码,故不能用拼接字符串获取URL的方式去做到遍历全国所有城市的天气。所以只能手动…就像这样: 只能每个城市单独一个URL,代码复用性不高…希望小伙伴们有好的解决方案可以及时告诉我呀!爱你们!!!
|