【问题标题】:sec.gov scraping with nested for-loops running into Unicode Errorsec.gov 使用嵌套的 for 循环进行抓取,遇到 Unicode 错误
【发布时间】:2021-04-05 12:41:03
【问题描述】:

我正在尝试获取 sec 以获取通过输入选择的公司的所有 10-K 归档链接。 该程序从 1993 年至今每年的每个季度(QTR1-4)循环。 我从https://codingandfun.com/scraping-sec-edgar-python/ 得到了代码 执行时遇到:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 13013584: invalid continuation byte 如果没有我多年来的 for 循环和固定的年份/季度,它可以工作 - 那么这里的问题是什么?

import bs4 as bs
import requests
import pandas as pd
import re
from datetime import datetime

def get_base():
    company = input('Which company?: ')
    filing = '10-K'
    year = [*range(1993,datetime.now().year + 1)]
    quarter = ['QTR1','QTR2','QTR3','QTR4']
    #get all filings for each quarter(QTR1-4) in each year(beginning 1993 until actual year)
    for x in year:
        for y in quarter:
            download = requests.get(f'https://www.sec.gov/Archives/edgar/full-index/{x}/{y}/master.idx').content
            download = download.decode("utf-8").split('\n')
            for item in download:
                #company name and report type
                if (company in item) and (filing in item): 
                   
                    company = item
                    company = company.strip()
                    splitted_company = company.split('|')
                    url = splitted_company[-1]
                    
                    #build second part of the url
                    url2 = url.split('-') 
                    url2 = url2[0] + url2[1] + url2[2]
                    url2 = url2.split('.txt')[0] 

                    # build third part of the url
                    to_get_html_site = 'https://www.sec.gov/Archives/' + url
                    data = requests.get(to_get_html_site).content
                    data = data.decode("utf-8") 
                    data = data.split('FILENAME>')
                    data = data[1].split('\n')[0]

                    #combine
                    url_to_use = 'https://www.sec.gov/Archives/'+ url2 + '/'+data
                    print(url_to_use)
                    

get_base()

【问题讨论】:

  • 10-K 是年度申报。如果您正在寻找那些,为什么要骑自行车穿过宿舍?
  • 主要是因为我不知道文件发布的季度。这是第四季度或第一季度。稍后我也想使用季度数据
  • 您不需要知道季度 - 一年只有一个 10-K。
  • 是的,我明白,但我想稍后再获得 10-Q,我无法按年份搜索。您需要年份和季度来搜索主文件。一些公司在 2020 年第四季度共享 2020 年 10-k,而一些公司在 2021 年第一季度共享它
  • 我不知道你是否知道,但是一些 edgar API 包装器已经存在于 python 中。 (github.com/edgarminers/python-edgargithub.com/joeyism/py-edgar ...)。或者是为了练习?

标签: python web-scraping beautifulsoup python-requests sec


【解决方案1】:

您需要指定标题:

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}

download = requests.get(f'https://www.sec.gov/Archives/edgar/full-index/{x}/{y}/master.idx', headers=headers).text

在行尾将 .content 更改为 .text。 对于下载文件的过滤器标题,将过滤器视为:

data = [row.split('|') for row in download.split("\n") if '|' in row]
data_10k = [row for row in data if row[2] == "10-K"]
data_10Q = [row for row in data if row[2] == "10-Q"]

我希望这能够让您在工作中畅通无阻,但我认为这不是一种有效的方式来做您想做的事情。 (可能很长,你需要知道公司的确切名称......)

【讨论】:

  • 非常感谢您的建议。最大的问题是它的速度是对的!我真的没有找到一个好的/好的方法来刮秒。我也在研究其他解决方案,但我从 3 个月开始学习 python - 所以这对我来说并不容易。我想稍后构建一个 Dash Dashboard,您可以在其中在基于建议的搜索栏中搜索公司 - 因此公司名称或 CIK 不会成为问题。如果您有任何想法,请随时与我联系。我认为这是一个可以边做边学的大项目
  • 如果您的入口点是公司名称,您应该使用 sec 提供的建议工具:sec.gov/edgar/searchedgar/companysearch.html。您需要学习如何轻松使用浏览器的开发工具(F12 -> 网络 -> 等)。学习如何处理经济、定量和文本数据以及 Web API 概念确实是一个有趣的项目。玩得开心;)
猜你喜欢
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
相关资源
最近更新 更多