【问题标题】:In Python 3, requests.get() gives incomplete json response在 Python 3 中,requests.get() 给出了不完整的 json 响应
【发布时间】:2018-07-31 22:48:52
【问题描述】:

我正在尝试使用requests.get() 获取数据。并且响应数据很大(包含 10000 条 mongodb 记录)。但我得到的回应几乎总是被打破。我得到正确结果的次数很少。

示例:
应该是这样的:

[
    {
        "_id":"5a72c839c634133e1e9ab502",
        "data":{"today_wh":13500},
        "dts":"2018-02-01T07:56:31.000Z",
        "ts":1517471791
    },
    {
        "_id":"5a72c839c634133e1e9ab503",
        "data":{"today_wh":13500},
        "dts":"2018-02-01T07:57:06.000Z",
        "ts":1517471826
    }
]

如下:

[
    {
        "_id":"5a72c8ecc634133e1e9ab51b",
        "data":{"today_wh":13700},
        "dts":"2018-02-01T08:00:01.000Z",
        "ts":1517472001
    },
    {
        "_id":

怎样做才能得到完整的结果?

【问题讨论】:

  • 显示您用于发出和接收请求的代码。
  • @martinho - 这个问题解决了吗?

标签: python python-3.x python-requests


【解决方案1】:

requests 设置的默认 User-Agent 是 'User-Agent': 'python-requests/2.7.6'。尝试模拟它来自浏览器而不是脚本。尝试如下模拟 User-Agent:

import requests
url = "http://example.com/"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
    'Content-Type': 'text/html',
}
response = requests.get(url, headers=headers)
html = response.text

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多