使用python在django中导入请求库有什么解决方案吗?
我试图在Django中链接我的python代码,因为我正在使用天气API,并使用它提取任何国家的数据 我想使用API链接中的位置、日期、格式和tp。问题是我不知道如何从同一个链接中提取这四个特定的东西。它告诉我:使用python在django中导入请求库有什么解决方案吗?,python,django,Python,Django,我试图在Django中链接我的python代码,因为我正在使用天气API,并使用它提取任何国家的数据 我想使用API链接中的位置、日期、格式和tp。问题是我不知道如何从同一个链接中提取这四个特定的东西。它告诉我: Traceback (most recent call last): File "C:\Users\Muahr\source\repos\RCAI-Project\Pest\api.py", line 14, in <module> url=int(api_ad
Traceback (most recent call last):
File "C:\Users\Muahr\source\repos\RCAI-Project\Pest\api.py", line 14, in <module>
url=int(api_address+city+24+date_time)
TypeError: must be str, not int
代码的问题:
date_time = datetime.strptime(ask, date_format)
url=int(api_address+city+24+date_time)
不能将str、int和datetime与+连接起来。它们必须都是字符串。
您可以先将它们转换为字符串,但由于datetime是从输入中的字符串开始的,所以您不需要首先解析它。
结果显然不能转换为int。
您不希望URL使用int。
结果仍然远远不是一个格式正确的URL,在正确的位置使用&和=来表示参数不会神奇地将参数插入所需的位置。您需要执行以下操作:
上述情况并不好。请求可以通过params为您处理此问题。
这个问题与使用请求或Django没有任何关系。为什么要将URL转换为int?那意味着什么?为什么要在URL的其余部分的字符串后面附加int 24?
import requests
city = input("enter city\n")
date = input("enter date\n")
json_data = requests.get(
'http://api.worldweatheronline.com/premium/v1/past-weather.ashx',
params=dict(
key='abc123...',
q=city,
format='json',
date=date,
tp='24'
)
).json()
formatted_data = json_data['data']
print(formatted_data)
date_time = datetime.strptime(ask, date_format)
url=int(api_address+city+24+date_time)
url = ('http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=abc123&q='
+ city + '&format=json&date=' + ask + '&tp=24')