如何编写python脚本来调用API并打印从API检索到的结果


How to write a python script to call a API and print the results retrieved from the API

我是一个初学者和新的编码。我有一个API在php给出了一些气象数据从气象站检索。我想有一个python脚本,将调用API和打印结果从API在一个文件。请建议我正确的编码方法。API是:http://ewodr.wodr.poznan.pl/doradztwo/swd/meteo_api.php?dane={"令牌":"电脑"、"id":303年,"operacja":"odczyt_doba"}

使用urllib2库(适用于python 2)从API获取数据。它可以看起来像这样:

import urllib2
import csv  
        # API url
        url = "your API url"
        # open the api url
        response = urllib2.urlopen(str(url))
        data = response.read()
        # write the results in a csv file
        with open('file.csv', 'wb') as f:
            f.write(data)

或JSON

import json
import urllib2  
        # API url
        url = "your API url"
        # open the api url
        response = urllib2.urlopen(str(url))
        data = json.load(response) 
        # write the results in a csv file
        with open('file.txt', 'w') as f:
            json.dump(data, f)

我没有在python 3中使用它,但我不认为会有很多变化。