【发布时间】:2023-03-20 09:06:01
【问题描述】:
我正在做一些抓取并查看类似这样的页面 (https://www.endomondo.com/rest/v1/users/20261627/workouts/526622897),但我无法完全检索 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。例如,在这种情况下: https://www.endomondo.com/rest/v1/users/20261627/workouts/526622897
我收到了:
{'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,它也很好 - 除非我与原始文件进行比较,否则它不会显示任何丢失的迹象。
如果您能提供任何有关如何解决此问题的建议,我将不胜感激。谢谢。
【问题讨论】:
标签: python json python-requests