Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3请求模块或urllib.request模块都检索不完整的json_Python_Json_Python Requests - Fatal编程技术网

Python3请求模块或urllib.request模块都检索不完整的json

Python3请求模块或urllib.request模块都检索不完整的json,python,json,python-requests,Python,Json,Python Requests,我正在进行一些清理并查看类似以下()的页面,但我无法完全检索JSON内容。我尝试使用以下两组代码,但每一组都返回一个不完整的JSON对象: url = 'https://www.endomondo.com/rest/v1/users/%s/workouts/%s'%(string_use_user, string_use_workout) print(url) response = urlopen(url) try: reader = codecs

我正在进行一些清理并查看类似以下()的页面,但我无法完全检索JSON内容。我尝试使用以下两组代码,但每一组都返回一个不完整的JSON对象:

    url = 'https://www.endomondo.com/rest/v1/users/%s/workouts/%s'%(string_use_user, string_use_workout)
    print(url)
    response = urlopen(url)
    try:
        reader = codecs.getreader("utf-8")
        print(reader(response))
        jsonresponse = json.load(reader(response))
        print(jsonresponse)
同样,使用响应库而不是urllib也无法检索完整的JSON

    url = 'https://www.endomondo.com/rest/v1/users/%s/workouts/%s'%(string_use_user, string_use_workout)
    print("using this url %s"%url)
    r = requests.get(url)
    try:
        print(r.json())
        jsonresponse = r.json()# json.loads(response.read())
在这两种情况下,我得到了大约1/4的JSON。例如,在这种情况下:

我收到:

{'feed_id': 281475471235835, 'id': 526622897, 'duration': 4082.0, 'local_start_time': '2015-05-21T09:30:45.000+02:00', 'calories': 1073.0, 'tagged_users': [], 'altitude_max': 69.9523, 'sport': 0, 'distance': 11.115419387817383, 'altitud\
e_min': 14.9908, 'include_in_stats': True, 'hydration': 0.545339, 'start_time': '2015-05-21T07:30:45.000Z', 'ascent': 137.162, 'is_live': False, 'pb_count': 2, 'playlist': [], 'is_peptalk_allowed': False, 'weather': {'wind_speed': 11, '\
temperature': 12, 'wind_direction': 13, 'type': 3, 'humidity': 81}, 'speed_max': 24.8596, 'author': {'name': 'gfdgfd', 'id': 20261627, 'last_name': 'gdsgsk', 'gender': 0, 'expand': 'abs', 'picture': {'url': 'https://www.endom\
ondo.com/resources/gfx/picture/18511427/thumbnail.jpg'}, 'first_name': 'gdsgds', 'viewer_friendship': 1, 'is_premium': False}, 'sharing': [{'share_time': '2015-05-21T08:45:19.000Z', 'type': 0, 'share_id': 1635690786663532}], 'show_map':\
 0, 'pictures': [], 'hashtags': [], 'descent': 150.621, 'speed_avg': 9.80291763746756, 'expand': 'full', 'show_workout': 0, 'points': {'expand': 'ref', 'id': 2199549878449}}
我没有收到数据中的长数组。我甚至没有恢复所有非数组数据

我通过JSON验证程序运行了原始页面,一切正常。类似地,我运行了通过验证器接收到的JSON,这也很好——除非与原始版本进行比较,否则它不会显示任何缺失的迹象


如果您能提供有关如何解决此问题的任何建议,我将不胜感激。谢谢。

看起来这个API正在做一些工作,并且只发送它认为是实际web浏览器的完整内容

使用通用浏览器的UA字符串设置
用户代理
标题后,您将获得完整响应:

>>> UA = 'Mozilla/5.0 (X11; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0'
>>> url = 'https://www.endomondo.com/rest/v1/users/20261627/workouts/526622897'
>>> r = requests.get(url, headers={'User-Agent': UA})
>>>
>>> print len(r.content)
96412
请参阅
请求
文档以了解有关的更多详细信息