# 建立连接 client = pymongo.MongoClient('192.168.1.123',27000) # 创建数据库 book_weather=client['weather'] # 创建表 sheet_weather=book_weather['sheet_weather_3'] url = 'https://a.hecdn.net/download/dev/china-city-list.csv' strhtml = requests.get(url) data = strhtml.text datal = data.split("\n") for i inrange(3): datal.remove(datal[0]) for item in datal: url = 'https://free-api.heweather.net/s6/weather/now?location=' + item[0:11] +'&key=xxx' strhtml=requests.get(url) time.sleep(1) dic = strhtml.json() # 向数据库中写入数据 sheet_weather.insert_one(dic)
查询北京的天气数据:
1 2 3 4 5 6 7
import pymongo
client = pymongo.MongoClient('192.168.1.123',27000) book_weather = client['weather'] sheet_weather = book_weather['sheet_weather_3'] for i in sheet_weather.find({'HeWwather6.basic.city':'北京'}): print(i)