1

我目前有一个脚本应该获取并返回 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 linkis 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

先感谢您。

4

3 回答 3

6

此响应是 json,因此,解码 json 而不是尝试解析字符串。

>>> import json
>>> resp = '{"status_code": 200, "data": {"units": -1, "tz_offset": -4, "unit": "day", "link_clicks": 535}, "status_txt": "OK"}'
>>> resp_object = json.loads(resp)
>>> resp_object and resp_object.get('data', {}).get('link_clicks', 0) or 0
535
于 2012-10-31T20:30:10.103 回答
2

src看起来像它的JSON。为什么不用json模块直接读取呢?

无论出于何种原因,如果您不想使用json,请继续阅读:

该错误是由于您假设您拆分的子字符串存在于您拆分的字符串中,即'clicks": '确实是src. 如果不是这种情况(因为我怀疑它不是在引发错误时),则split返回一个列表,其中只有一个元素,并且该元素是src.

如果您更喜欢在这种情况下,那src[1]应该在调用后给您一个空字符串src = src.split('clicks": '),那么您最好使用str.partition

In [5]: somestr = 'prefixclicks: "suffix'

In [6]: somestr.partition('clicks: "')
Out[6]: ('prefix', 'clicks: "', 'suffix')

In [7]: somestr.partition('clicks: "')[-1]
Out[7]: 'suffix'

In [8]: somestr = 'prefixsuffix'

In [9]: somestr.partition('clicks: "')
Out[9]: ('prefixsuffix', '', '')

In [10]: somestr.partition('clicks: "')[-1]
Out[10]: ''

希望这可以帮助

于 2012-10-31T20:37:43.143 回答
0

你可以试试这个来找出 的值link_clicks,使用ast.literal_eval()

In [14]: import ast

In [15]: src=`{"status_code": 200, "data": {"units": -1, "tz_offset": -4, "unit": "day", "link_clicks": 535}, "status_txt": "OK"}`

In [16]: d=ast.literal_eval(src)

In [17]: d["data"]["link_clicks"]
Out[17]: 535
于 2012-10-31T20:34:51.230 回答