作者:三人行 | 来源:互联网 | 2023-02-12 12:08
#-*-encoding:utf-8-*-importrequestsimportpandasaspdimporttimeimportpymongo#导入mongodb的库#建立链
# -*- encoding: utf-8 -*-
import requests
import pandas as pd
import time
import pymongo #导入mongodb的库
#建立链接
client = pymongo.MongoClient('localhost',27017)
#创建数据库
book_weather= client['weather']
#在weather库中建表sheetweather表
sheet_weather_now=book_weather['sheet_weather_now']
print(client)
url='https://cdn.heweather.com/china-city-list.txt'
strhtml= requests.get(url)
strhtml.encoding= 'utf-8' #把requests对象进行编码转换,否则乱码。
data= strhtml.text
data=data.replace(' ','') #去掉data数据中的空格,否则再使用pandas获取城市ID时找不到列名
#把数据以文本形式存到代码目录
with open('./data.txt','w',encoding='utf-8') as f:
f.write(data)
#读取data.txt数据作为dataframe对象,以便后续遍历城市ID↓
df=pd.read_csv('./data.txt',header=3,sep='|').dropna(axis=1) #dorpna 1是按行删除空值列
# print(df['城市ID'])
for id in df['城市ID'][1:]:
print("id:",id)
#根据网站提供的API接口文档写访问的URL 第一个:3天预测;第二个:今天天气;第三个:城市信息
url_3d ="https://devapi.qweather.com/v7/weather/3d?location="+id+"&key=d910b0c0ca62429a9e9e278ae3f3276c"
url_weather = "https://devapi.qweather.com/v7/weather/now?location="+id+"&key=d910b0c0ca62429a9e9e278ae3f3276c"
url_city = "https://geoapi.qweather.com/v2/city/lookup?location="+id+"&key=d910b0c0ca62429a9e9e278ae3f3276c"
print(url_weather)
weather_results=requests.get(url_weather)
time.sleep(2) #每2秒读取一次数据
# print(weather_results.text)#打印读取内容
dic= weather_results.json()
#打印3天里的第一天预报
print(dic['now'])
sheet_weather_now.insert_one(dic['now'])
# print(str(dic['daily'][0]).replace(' ',''))