Python3 requests 模块或 urllib.request 模块均检索不完整的 json

标签 python json python-requests

我正在做一些抓取并查看像这样的页面( 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,它也很好 - 除非与原始数据进行比较,否则它不会显示任何丢失内容的迹象。

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

最佳答案

看起来这个 API 正在做一些事情 User-Agent sniffing并且仅发送其认为是实际网络浏览器的完整内容。

一旦您使用常见浏览器的 UA 字符串设置 User-Agent header ,您将获得完整的响应:

>>> 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

有关 setting custom headers 的更多详细信息,请参阅 requests 文档.

关于Python3 requests 模块或 urllib.request 模块均检索不完整的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30855489/

相关文章:

python - 使用列表理解输出嵌套列表

python - 可解构在 Python 中有什么作用?

python:我可以在不实际安装 python 的情况下运行 python 脚本吗?

php - json_decode 到自定义类

python - 使用请求通过http协议(protocol)将设置发送到clickhouse

python - Python 的 GIL 和垃圾收集器有什么关系?

javascript - json 值的 jstree 给出了一些错误

json - Swift 和 Codable : Encodable tree struct terminating in values that obey custom protocol

Python Requests-Cache 仍在查询远程 URL

python-3.x - 检查大文件是否存在而不下载它