sardine2 / python

Life is short, I need python.
0 stars 0 forks source link

ch3 API #15

Open sardine2 opened 7 years ago

sardine2 commented 7 years ago

如何更新 pip install requests --upgrade

pip3 install pypinyin pip3 install requests

调用 openweathermap 国外api时可用 pypinyin 用于汉字转拼音

sardine2 commented 7 years ago

here are the important information of ch3 task:

  1. requests

    Passing Parameters In URLs You often want to send some sort of data in the URL's query string. If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g. httpbin.org/get?key=val. Requests allows you to provide these arguments as a dictionary of strings, using the params keyword argument. As an example, if you wanted to pass key1=value1 and key2=value2 to httpbin.org/get, you would use the following code:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get('http://httpbin.org/get', params=payload)

2.openweathermap

Format Description:

API call:

http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID={APIKEY} Parameters:

APPID {APIKEY} is your unique API key Example of API call:

api.openweathermap.org/data/2.5/forecast?id=524901&APPID=1111111111

I think maybe this: the id is equally to the city name that == 'input'

so payload = {'key1': 'value1', 'key2': 'value2'} == payload = {'q': i, 'APPID': '1111111111'}


JSON format is used by default. To get data in XML or HTML formats just set up mode = xml or html.

Parameters:

mode - possible values are xml and html. If mode parameter is empty the format is JSON by default. Examples of API calls:

JSON api.openweathermap.org/data/2.5/weather?q=London

XML api.openweathermap.org/data/2.5/weather?q=London&mode=xml

HTML api.openweathermap.org/data/2.5/weather?q=London&mode=html

这里有个 XML,那么是否可以直接用在微信?(啊我的「大象跑了」貌似有希望修好?)


Description: You can use lang parameter to get the output in your language. We support the following languages that you can use with the corresponded lang values:

English - en, Chinese Simplified - zh_cn, Chinese Traditional - zh_tw. NOTE: Translation is only applied for the "description" field.

API call: http://api.openweathermap.org/data/2.5/forecast/daily?id=524901&lang={lang} Parameters:

lang language code Examples of API calls:

http://api.openweathermap.org/data/2.5/forecast/daily?id=524901&lang=zh_cn

sardine2 commented 7 years ago

shippomiru 同学用了 set() 来解决历史数据重复查询这个问题。我虽然从ch1就有考虑这个问题,但却一直没有动手 ... 好惭愧。。。 set_doc Python Sets

elif key == 'history':
            print('查询记录:')
            for i in set(historyList):
                print(i)
sardine2 commented 7 years ago

JSON:

主要包含四个方法: dump和dumps(从python生成json),load和loads(解析json成python的数据类型)dump和dumps的唯一区别是 dump会生成一个类文件对象,dumps会生成字符串,同理load和loads分别解析类文件对象和字符串格式的json。

sardine2 commented 7 years ago

5 day respond forecast of openweathermap :

{'cod': '200', 
'message': 0.0144,
'cnt': 35, 
'list': [     
{'dt': 1494860400,
'main': {'temp': 16.94, 'temp_min': 16.04, 'temp_max': 16.94, 'pressure': 1030.57, 
'sea_level': 1038.13, 'grnd_level': 1030.57, 'humidity': 78, 'temp_kf': 0.9},
'weather': [{'id': 500, 'main': 'Rain', 'description': '小雨', 'icon': '10d'}],
'clouds': {'all': 88},
'wind': {'speed': 5.89, 'deg': 210.011}, 
'rain': {'3h': 0.065}, 
'sys': {'pod': 'd'},
'dt_txt': '2017-05-15 15:00:00'}, 

{...},{..},{34...},{35..}], 
'city': {'id': 2643743, 'name': 'London',
 'coord': {'lat': 51.5085, 'lon': -0.1258}, 'country': 'GB'}  }
sardine2 commented 7 years ago

北京袁小丽同学用高德地图api 取得经纬度的方式也很赞~

经纬度函数:调用高德地图 API ,输入城市,输出经纬度

def geocode(address):
    parameters = {'address': address, 'key': 'a7d549571aff546b57285591bc41003a'}
    base = 'http://restapi.amap.com/v3/geocode/geo'
    response = requests.get(base, parameters)
    answer = response.json()
    lng_lat = answer['geocodes'][0]['location']
    print(address + "的经纬度:", answer['geocodes'][0]['location'])
    # print(answer['geocodes'][0]['location'])
    return lng_lat
coordinate = geocode(address)
        url1 = "https://api.caiyunapp.com/v2/IdH0=8xDRi9Hatis/%s/realtime.json"%coordinate
        # print(url1)   
        s = requests.get(url1)
        js = s.json()
        weather_inquiry(js)
sardine2 commented 7 years ago

leiyunhe:code

爬取数据BeautifulSoup

从彩云wiki中获得城市与经纬度的对照表,需要下载表格,选择“城市”和“经纬度”两列数据,保存到字典中。这可通过Beautiful Soup模块实现。首先,安装模块,和其他的第三方模块一样,可以下载最新版源代码bs4.1.0到python3的目录中,(旧版本不支持Python3,使用中会报错。) 接着,使用bs模块中的方法提取html标签中符合要求的数据项。这个过程中,需考虑不同数据的数据类型,才能正确处理。其次,html标签和内容是不同的概念。提取内容的时候,需专门获取tag中字符串。在CLI中print显示出来的内容与实际的内容不是一回事。

sardine2 commented 7 years ago

ch3算是告一段落了?毕竟已经做到查询五天每天数据和一次查询五天,历史的记录也算正常了。