Scraping EDGAR with Python code (Program 2) results in TypeError: a bytes-like object is required, not 'str'
18:35 13 Nov 2020

I am trying to get the (10-K) filing of Amazon in the year of 2013. My Python program for this is not working, perhaps due to the Python version difference between Python 2's urllib2 and Python 3's urllib, I suppose.

My problem is that I encountered the initial error:

TypeError: a bytes-like object is required, not 'str'

Please help me to get the result of "element4" other than "None" for the code and error below;

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

--- Error Message ---


TypeError                                 Traceback (most recent call last)

 in 

     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'
python python-3.x web-scraping url edgar