|

天气预报接口:http://www.weather.com.cn/data/sk/101010100.html
(101010100是北京天气的代码)
接口json字段说明:
XML接口根节点: http://flash.weather.com.cn/wmaps/xml/china.xmlXML
接口主要作用是递归获取全国几千个县以上单位的城市代码,如:上海的XML地址为:http://flash.weather.com.cn/wmaps/xml/shanghai.xml 山东的XML地址为:http://flash.weather.com.cn/wmaps/xml/shandong.xml
获取全国+国外主要城市,8763个城市列表信息。URL:http://mobile.weather.com.cn/js/citylist.xml
步骤:先获取到全部城市的列表的代码,然后,再通过城市名称获取对应的代码,访问天气接口,获取该城市的天气。
python代码如下:
结果:
C:\project\web\app1>cd c:\project\web\app1 && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && python C:\Users\itsvse_pc\.vscode\extensions\ms-python.python-2018.4.0\pythonFiles\PythonTools\visualstudio_py_launcher_nodebug.py c:\project\web\app1 56150 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput c:\project\py\demo.py "
http://www.itsvse.com by:小渣渣
开始处理数据处理完成!
获取天气成功
{'weatherinfo': {'city': '济宁', 'cityid': '101120701', 'temp': '23.6', 'WD': '南风', 'WS': '小于3级', 'SD': '67%', 'AP': '1002.3hPa', 'njd': '暂无实况', 'WSE': '<3', 'time': '18:00', 'sm': '1.4', 'isRadar': '0', 'Radar': ''}}
济宁天气
温度:23.6
风向:南风
风力:小于3级
参考资料
Python实现XML文件解析:https://www.itsvse.com/thread-4823-1-1.html
python爬虫之requests的基本使用:https://www.cnblogs.com/lei0213/p/6957508.html
最后,附上代码:
demo.py
(1.85 KB, 下载次数: 2)
|
上一篇:VS Code 代码快速格式化快捷键下一篇:TCP端口通信连接demo
|