我尝试了 Rasha Ashraf 文章“Scraping EDGAR with Python”中的 python 代码。昨天我得到了你们伟大的开发人员的帮助。特别感谢杰克·弗利廷。 该问题相关链接如下:
文本抓取(来自 EDGAR 10K Amazon)代码不起作用
这是上面同一篇文章中的第二个 Python 程序,但仍然......由于 Python 版本差异而无法工作,我想。
我的问题是我遇到了名为“TypeError: a bytes-like object is required, not 'str'”的初始错误。我搜索了 StackOverflow 并应用了一种又一种方法。然而,一旦一条错误消息消失,其他错误就会发生。在我临时修改了多个代码后,“print(element4)”的结果显示“None”。这不是作者想要的结果。
我纠正原始代码的微不足道的尝试被证明是行不通的。因此,我在这里上传原始代码和第一条错误消息。一旦你帮助我解决了最初的错误消息,那么我将继续解决第二个、第三个等等。
我通常使用 Python 处理 CSV 文件格式的数值变量和分类变量。因此,这个 Web 抓取 Python 程序(尤其是处理和收集 URL)在某种意义上目前超出了我的能力范围。请帮助我获得除“None”之外的“element4”的结果。这样我就可以得到2013年亚马逊(10-K)备案的正确路径了。
import time
import csv
import sys
CIK = '1018724'
Year= '2013'
FILE= '10-K'
# Get the Master Index File for the given Year
url='https://www.sec.gov/Archives/edgar/full-index/%s/QTR1/master.idx'%(Year)
from urllib.request import urlopen
response= urlopen(url)
string_match1= 'edgar/data/'
element2 = None
element3 = None
element4 = None
# Go through each line of the master index file and find given CIK # and File (10-K)
# and extract the text file path
for line in response:
if CIK in line and FILE in line:
for element in line.split(' '):
if string_match1 in element:
element2 = element.split('|')
for element3 in element2:
if string_match1 in element3:
element4 = element3
print(element4)
### The path of the 10-K filing
url3 = 'https://www.sec.gov/Archives/'+element4
--- 错误信息 ---
TypeError Traceback (most recent call last)
<ipython-input-25-8b7ded22bf96> in <module>
25
26 for line in response:
---> 27 if CIK in line and FILE in line:
28 for element in line.split(' '):
29 if string_match1 in element:
TypeError: a bytes-like object is required, not 'str'
我相信这就是您正在寻找的:
import requests
import csv
CIK = '1018724'
Year= '2013'
FILE= '10-K'
url='https://www.sec.gov/Archives/edgar/full-index/%s/QTR1/master.idx'%(Year)
req = requests.get(url)
targets = csv.reader(req.text.splitlines(), delimiter='|')
for line in targets:
if CIK in line and FILE in line:
print("https://www.sec.gov/Archives/"+line[-1])
输出:
https://www.sec.gov/Archives/edgar/data/1018724/0001193125-13-028520.txt