我目前有一个脚本应该获取并返回 Bit.ly 链接的点击次数。我首先从 Bitly url 收集和读取数据,这似乎是正确的。
bitly_data = "https://api-ssl.bitly.com/v3/link/clicks?access_token=ACCESS_TOKEN&link=http://bit.ly/"+link
src = urllib2.urlopen(bitly_data)
src = src.read()
When link
is something such as TY8lnd
,src
是一个看起来像的字符串
{“status_code”:200,“data”:{“units”:-1,“tz_offset”:-4,“unit”:“day”,“link_clicks”:535},“status_txt”:“OK”}
我现在想解析这个字符串以获取 . 之后的数值link_clicks
。我认为最好的方法是进行两次拆分。
src=src.split('clicks": ')
src = str(src[1])
clicks = src.split('}, "status')
clicks = clicks[0]
当我运行它时,clicks 最终会等于正确的数字,并且仅此而已。但是,终端为 line 返回一个 IndexError src = str(src[1])
。我试图摆脱,str()
但这没有效果。尽管最终值已得到纠正,但如果能理解为什么我会收到此错误,将不胜感激。
这是完整的追溯:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Zach/Dropbox/bitly/bit.py", line 35, in settings
src = str(src[1])
IndexError: list index out of range
先感谢您。