本文主要是介绍爬虫之和风天气,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#coding=utf-8
import requests
import time'''和风天气数据爬取
'''def get_weather(city=None):url='https://free-api.heweather.com/v5/forecast?city={}&key=7d0daf2a85f64736a42261161cd3060b'.format(city)response = requests.get(url)time.sleep(2) # 延时2秒,防止被抓info = response.json()dic = info['HeWeather5'][0]['daily_forecast']for item in dic:print(item['tmp']['max'])def get_city_code():url = 'https://cdn.heweather.com/china-city-list.txt'response = requests.get(url)response.encoding='utf-8'data = response.textdatas = data.split('\n')for i in range(6): # 清除文档前多余行datas.remove(datas[0])for item in datas: # 逐行解析item = list(item.strip(' ').split('|')) # 单条数据列表化get_weather(item[1].strip(' ')) # 清除前后空格if __name__ == '__main__':get_city_code()
这篇关于爬虫之和风天气的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!