我有一些代码可以将今天的日期显示为一个字符串,使用
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:只能将字符串(而不是"datetime.date")连接到字符串
发布于 2019-11-17 08:54:46
只需使用
str(currnetdate)
将datetime对象转换为字符串。您也可以使用f字符串。
发布于 2019-11-17 08:55:54
不能将date
连接到str
。如果使用Python 3.6或更新版本,请使用f-string
main_api = f'https://www.sydneyairport.com.au/_a/flights/?query=&flightType=departure&terminalType=domestic&date={currnetdate}&sortColumn=scheduled_time&ascending=true&showAll=true'
否则,将currnetdate
强制转换为str
main_api = 'https://www.sydneyairport.com.au/_a/flights/?query=&flightType=departure&terminalType=domestic&date=' + str(currnetdate) + '&sortColumn=scheduled_time&ascending=true&showAll=true'
https://stackoverflow.com/questions/58898900
复制相似问题