采集当前天气, 全国2409个县的天气

1、http://www.nmc.cn/f/rest/real/57036?_=1525850063564 #这个是搜索西安, 然后在返回的页面里面找到 天气预报的url 1、1525850063564 是随机数, 不用管。寻找57036

2、搜索 57036 找到包含很多id(57036)这样的网页 http://www.nmc.cn/f/rest/province/ASN 搜索ASN

3、搜索ASN 找到包含很多类似于 ASN的网页 http://www.nmc.cn/f/rest/province 打开这个网页(就能找到很多省份简写, )

4、陕西地区的id 在这里面 http://www.nmc.cn/f/rest/province/ASN, 那么别的省份的id 就在 http://www.nmc.cn/f/rest/province/+ 省份缩写里面(类似于ASN)


5、http://www.nmc.cn/f/rest/province #这是找到的最终的url 这里面藏着每个省的信息 然后反向requests 就能拿到想要拿的信息

 

6、然后开始请求1.1 http://www.nmc.cn/f/rest/province 用来获得省份缩写 ,放在一个字典里面

         1.2 请求 http://www.nmc.cn/f/rest/province/+ 这个字典里面的values(省份缩写), 用来获得每个省份下面城市对应的id ,用字典储存(城市:id)


            url_random = str(random.randint(1500000000000, 1599999999999))

        1.3 请求 http://www.nmc.cn/f/rest/real/+id+?_= url_randon 这就拿到每一个城市对应天气的url


           1.4 循环请求 http://www.nmc.cn/f/rest/real/+id+?_= url_randon 用json.loads把str转成字典模式, 提取天气放到列表,


         1.5 进行打印, 储存










import re
import requests
import json
import random
import time

def get_url1(url1):
r = requests.get(url1)
dict_province = json.loads(r.text) #把字符串转成字典
#print(dict_province)
#print('dict_province:',type(dict_province))
city_dict = {} #记录每一个省份的缩写 (类型ABC)
for i in dict_province:
#i 里面是每一个字典
#print(i)
#给字典赋值
city_dict[i['name']] = i['code']
province_city(city_dict)


def province_city(city_dict):
for j in city_dict.values():
place_url = 'http://www.nmc.cn/f/rest/province/'+j
#print(place_url)
get_url(place_url) #把获取每一个省下面的每一个地区、


def get_url(url):
m = requests.get(url)
m.encoding = m.apparent_encoding
#print(m.text)
dict_place = json.loads(m.text) # 把字符串转成字典
#print('dict_place:',dict_place)
#print('dict_place:',type(dict_place))

#little_city_dict = {}
for i in dict_place:
# i 里面是每一个字典
# print(i)
# 给字典赋值
little_city_dict[i['city']] = i['code']

def weather_url(little_city_dict):
print('每个城市的天气:',little_city_dict)
for k in little_city_dict.values():
#http://www.nmc.cn/f/rest/real/57048?_=1525417963109
url_random = str(random.randint(1500000000000, 1599999999999))
weather_url = 'http://www.nmc.cn/f/rest/real/'+k+'?_='+url_random
#print(weather_url)
get_weather_url(weather_url)

def get_weather_url(weather_url):
try:
r = requests.get(weather_url)
if r.status_code == 200:
weather_json = json.loads(r.text) # 把字符串转成字典
#print("r.text:",r.text)
#print("weather_json:",weather_json)
li = []
li.append(weather_json['station']['province'])
li.append(weather_json['station']['city'])
li.append(weather_json['publish_time'])
li.append(weather_json['weather']['temperature'])
li.append(weather_json['weather']['humidity'])
li.append(weather_json['weather']['feelst'])
li.append(weather_json['wind']['power'])
li.append(weather_json['wind']['direct'])
print("li:", li)
else:
time.sleep(1)
get_weather_url(weather_url)
except Exception: # except BaseException 这个也可以
print("json问题")



if __name__ == '__main__':

little_city_dict = {}
url1 = 'http://www.nmc.cn/f/rest/province'
get_url1(url1) #requests省份
#print("每个城市的名称和id——little_city_dict:",little_city_dict)
list_num = little_city_dict.items()
num = 0
for m in list_num:
num += 1
print("一共有:",num)
weather_url(little_city_dict)

 

posted on 2018-05-09 10:55  袁佳佳  阅读(200)  评论(0编辑  收藏  举报

导航