首页 > 解决方案 > 如何将日期字符串添加到 URL?

问题描述

我有一些代码将今天的日期作为字符串使用

currnetdate = (pd.datetime.now().date())

然后在 URL 中,我有

main_api = 'https://www.sydneyairport.com.au/_a/flights/?query=&flightType=departure&terminalType=domestic&date=' + currnetdate + '&sortColumn=scheduled_time&ascending=true&showAll=true'

现在,当我运行代码时,我不断收到错误 TypeError: can only concatenate str (not "datetime.date") to str

标签: pythonstringpandasdatetimeurl

解决方案


只需使用

str(currnetdate)

将日期时间对象转换为字符串。您也可以使用 f 字符串。


推荐阅读