我正在尝试将字符串解析为 python datetime 对象,这是代码:
created = datetime.strptime(r.json()[x]["created_at"], "%a %b %d %H:%M:%S +0000 %Y")
我得到的错误是:
ValueError: time data '"Wed Jan 16 22:08:18 +0000 2013"' does not match format '%a %b %d %H:%M:%S +0000 %Y'
它应该是正确的,显然我做错了什么。请注意,r.json 以 unicode 格式返回文本,但我也尝试使用 str() 转换。
完整程序:
import requests
from datetime import datetime
from simplejson import dumps
url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name='
username = 'gtklondike'
count = '5'
url += username + "&count=" + count
r = requests.get(url)
x = 0
while x < count:
print "<div id='tw-body'>" + r.json()[x]["text"] + "</div>"
created = datetime.strptime(dumps(r.json()[x]["created_at"]), "%a %b %d %H:%M:%S +0000 %Y")
print "<div id='tw-date'>Date: " + r.json()[x]["created_at"] + "</div><br />\n" # TMP --> until I get datetime working... but at least it shows you the date
x = x + 1