请帮忙,SEC EDGAR 以前一直工作得很好,直到现在。它给出 HTTPError: HTTP Error 403: Forbidden
import pandas as pd
tables = pd.read_html("https://www.sec.gov/Archives/edgar/data/1541617/000110465920125814/xslForm13F_X01/infotable.xml")
df=tables[3]
df
该网站似乎拒绝了您的请求,因为它检测到该请求是自动执行的。如果您将标头
User-Agent: Mozilla/5.0
添加到 http 请求中,则可以绕过此问题,因为这会使该请求看起来像是来自 Firefox 浏览器。不幸的是,pd.read_html 不支持更改请求标头,因此我们必须使用 requests 库自己发出请求。
使用
pip install requests
安装请求
然后将代码更改为如下所示:
import pandas as pd
import requests
# Makes a request to the url
request = requests.get("https://www.sec.gov/Archives/edgar/data/1541617/000110465920125814/xslForm13F_X01/infotable.xml", headers={"User-Agent": "Mozilla/5.0"})
# Pass the html response into read_html
tables = pd.read_html(request.text)
df = tables[3]
print(df)
我注意到该网站的一件事是它不允许来自非住宅 IP 地址的请求,并且总是会给你一个 403。因此,如果你在云中的某个地方(例如 repl.it,通过 VPN)执行此代码或类似)此代码根本不起作用。不过,在我的家用计算机上运行该代码可以完美运行。该网站还表示,如果您每秒发出超过 10 个请求或总体请求量过多,它将阻止您的 IP 地址,因此请务必谨慎对待向网站发出请求的次数。
过去,只需添加用户代理即可修复此错误。
用户代理:示例公司名称[电子邮件受保护]
因此,与其使用真正的用户代理(例如从浏览器),不如使用:
Mozilla/5.0 (Company [email protected])