您当前的位置: 首页 >  Python

漏刻有时

暂无认证

  • 0浏览

    0关注

    717博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

python批量爬取高德地图天气webAPI接口并在本地生成txt文件

漏刻有时 发布时间:2021-05-27 21:49:07 ,浏览量:0

项目说明
  1. 导入requests模块,执行HTTP爬取命令;
  2. 导入json模块,对数据进行解码,文件f.write写入时,必须是string类型;
  3. with open as :文件操作语句;
  4. 列表类型;
  5. 构建天气封装函数;
import requests
import json

def getAreaWeather(code):
    url = 'https://restapi.amap.com/v3/weather/weatherInfo'
    r = requests.get(url, params={'city': '{}'.format(code), 'key': '4d9a765939a2b76588a3341c***'})
    # print(r.url)
    # print(r.text)
    res = json.loads(r.text)
    with open('{}.txt'.format(code), 'w+') as f:
        # 写入本地文件,并按区域城市代码命名
        f.write(json.dumps(res, ensure_ascii=False))

# 区域代码列表
area = ['330211', '330212', '330213']
# 执行循环爬取程序
for i in area:
    getAreaWeather(i)
print("爬取天气情况:SUCCESS")

lockdatav Done !

关注
打赏
1661217259
查看更多评论
立即登录/注册

微信扫码登录

0.0369s