【问题标题】:JSONDecodeError: Expecting value: line 1 column 1 (char 0) when scaping SEC EDGARJSONDecodeError: 期望值: scaping SEC EDGAR 时的第 1 行第 1 列 (char 0)
【发布时间】:2021-12-29 02:05:26
【问题描述】:

我的代码如下:

import requests
import urllib
from bs4 import BeautifulSoup

year_url = r"https://www.sec.gov/Archives/edgar/daily-index/2020/index.json"
year_content = requests.get(year_url)
decoded_year_url = year_content.json()

去年我可以运行完全相同的代码,但是当我昨天运行它时,弹出了警告: “JSONDecodeError:预期值:第 1 行第 1 列(字符 0)” 为什么?我应该如何解决问题?非常感谢!

【问题讨论】:

  • print(year_content.text),输出:We do not offer technical support for developing or debugging scripted downloading processes 出错
  • 网站不想为机器人服务。

标签: python edgar sec


【解决方案1】:

根据GitHub issue 从 2021 年 5 月起,显然 SEC 已在其网站上添加了速率限制。您收到错误消息的原因是响应包含 HTML,而不是 JSON,这会导致 @987654322 @ 在调用 .json() 时引发错误。

要解决此问题,您需要将User-agent 标头添加到您的请求中。我可以通过以下方式访问 JSON:

import requests
import urllib
from bs4 import BeautifulSoup

year_url = r"https://www.sec.gov/Archives/edgar/daily-index/2020/index.json"
year_content = requests.get(year_url, headers={'User-agent': '[specify user agent here]'})
decoded_year_url = year_content.json()

【讨论】:

    【解决方案2】:

    尝试导入 json 模块并使用方法 json.loads()

    import requests
    import urllib
    from bs4 import BeautifulSoup
    import json
    
    year_url = r"https://www.sec.gov/Archives/edgar/daily-index/2020/index.json"
    year_content = requests.get(year_url)
    decoded_year_url = json.loads(year_content)
    

    【讨论】:

    • 这不是 JSON 相关的问题。该请求正在返回 HTML 作为 403 Forbidden 响应的一部分。
    猜你喜欢
    • 2019-07-04
    • 2021-09-30
    • 2020-04-18
    • 2013-05-10
    • 2019-02-25
    • 2016-04-07
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多