爬虫练习案例(爬取高德地图中所有城市的天气信息)
代码如下:
"""
需求:爬取高德地图上面所有城市的天气信息
"""
# 导入需要的模块
import requests, json
# 定制请求头
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
# 定义获取adcode的url
adcode_url ='https://ditu.amap.com/service/cityList?version='
# 发送请求
adc_response = requests.get(url=adcode_url,headers=headers)
adc_result = adc_response.json()
# 获取cityBYletter数据
cityByletter = adc_result['data']['cityByLetter']
# 获取每个城市的天气信息的url
weather_url ='https://ditu.amap.com/service/weather?adcode={}'
# 循环遍历获取城市列表
for city_listin cityByletter.values():
# 循环出每一个城市的字典
for cityin city_list:
# 定义一个空字典,用于保存每个城市的数据
city_info = {}
# 通过键值对获取对应内容
# 获取城市的adcode
adcode = city['adcode']
# 获取城市的name
city_info['city_name'] = city['name']
# 将天气的url拼接好,发送请求
weather_response = requests.get(url=weather_url.format(adcode),headers=headers)
# 使用json.loads()将json转换成python类型,或使用weather_response.json()也可以-->weather_data = json.loads(weather_response.text)
weather_data = weather_response.json()
# 开始提取数据
zuiwai_data = weather_data['data']
# 获取result值
result = zuiwai_data['result']
# 判断resutl的值是否为true,如果是true,再进行提取
if result =='true':
# 获取forecast_data数据
forecast_data = zuiwai_data['data'][0]['forecast_data'][0]
# 获取天气名称
city_info['weather_name'] = forecast_data['weather_name']
# 获取最低气温
city_info['min_temp'] = forecast_data['min_temp']
# 获取最高气温
city_info['max_temp'] = forecast_data['max_temp']
# 写入txt文件
with open('gaode.txt','a',encoding='utf-8')as fp:
fp.write(str(city_info)+'\n')
print(city_info)
运行效果如下: