【问题标题】:unable to get Json data for the second time when requesting with URL使用 URL 请求时无法第二次获取 Json 数据
【发布时间】:2015-12-14 14:46:22
【问题描述】:

我有一个获取和处理JsonData 的函数。我的代码是这样的:

def getData(link,row):
    u=urllib2.urlopen(link).read();
    jsonObject=json.loads(u);
    # do some stuff
    return (jsonObject[u'pagination']['next_url'],row);

json,newRow=getData("https://api.instagram.com/v1/tags/****/media/recent?access_token=*****",0);
while (len(json )!= 0):
    json,newRow=getData(json,newRow);

当我第一次调用getData 函数时,它给了我正确的响应!但是当我在while 中第二次调用它时,我得到了这样的错误:

AttributeError: 'unicode' 对象没有属性 'loads'

有趣的是,第二次调用中的u 值是逻辑。它是这样的:

{"pagination":{"next_max_tag_id":"1139055135096994516","deprecation_warning":"next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id ............ }

为什么我在以后的函数调用中会出现这个错误?

【问题讨论】:

    标签: python json python-2.7 url


    【解决方案1】:

    您覆盖了json 定义。它从一个导入的模块开始,然后被重新定义为一个 unicode 字符串。

    尝试重命名变量:

    j,newRow=getData("https://api.instagram.com/v1/tags/****/media/recent?access_token=*****",0);
    while j:
        j,newRow=getData(j,newRow);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-05
      • 2023-04-10
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多