如何将时间python放在某个url中

如何将时间python放在某个url中,python,time,Python,Time,我想做以下工作: requestor = UrlRequestor("http://www.myurl.com/question?timestamp=", {'Content-Type': 'application/x-www-form-urlencoded', 'Cookie' : self.EASW_KEY + ";", 'X-UT-SID' :self.XUT_SID}, 'answer=' + self.securityHash) requestor.open() self.FUTPHI

我想做以下工作:

requestor = UrlRequestor("http://www.myurl.com/question?timestamp=", {'Content-Type': 'application/x-www-form-urlencoded', 'Cookie' : self.EASW_KEY + ";", 'X-UT-SID' :self.XUT_SID}, 'answer=' + self.securityHash)
requestor.open()
self.FUTPHISHING = requestor.getHeader('Set-Cookie').split(';')[0]
就在时间戳之后,我想要本地时间的格式:1355002344943


我如何才能做到这一点?

您可以从模块中获取该格式的时间。具体来说,我会这样做

import time

timeurl = "http://www.myurl.com/question?timestamp=%s" % time.time()
requestor = UrlRequestor(timeurl, {'Content-Type': 'application/x-www-form-urlencoded',      'Cookie' : self.EASW_KEY + ";", 'X-UT-SID' :self.XUT_SID}, 'answer=' + self.securityHash)
requestor.open()
self.FUTPHISHING = requestor.getHeader('Set-Cookie').split(';')[0]
time.time()返回一个浮点值,因此如果它不喜欢这个精度级别,可以这样做

timeurl = "http://www.myurl.com/question?timestamp=%s" % int(time.time())

该时间戳看起来以秒为基础(例如,自1970年1月1日以来的秒),但它还有三个数字。可能是毫秒,而不是秒。要复制它,我建议执行以下操作:

import time

timestamp = int(time.time()*1000)
url = "http://www.myurl.com/question?timestamp=%d" % timestamp
如果不想进行字符串格式设置,也可以简单地将时间戳连接到URL上:

url = "http://www.myurl.com/question?timestamp=" + str(timestamp)

非常感谢,但是需要$s吗?您也可以这样做
“http://www.myurl.com/question?timestamp={}.format(time.time())
这是Python中字符串格式化的新方法——不过,这是两种方法。Sam提出了一个很好的观点,.format()是首选方法。有一个很好的讨论,谢谢大家,这真的很有帮助,但是我真的需要在创建新变量之前输入timeurl=吗?@user203558:时间戳或url不需要单独的变量,但它们可以帮助减少
请求者
行的长度。一般来说,避免那些不能一次全部显示在编辑器屏幕上的行是一种很好的做法。