The House of Representatives provides downloadable text files of each title so I wrote the following Python script to grab them all and save them for use in my project.
import os
import urllib2
import codecs
ENCODING = 'utf-8' # saved files should be UTF-8 encoded
END_OF_TEXT = u'\u0003' # for use by Histrionics
url_stub = 'http://uscode.house.gov/download/pls/Title_{0}.txt'
save_dir = os.path.join('C:\Users\Bob User','cfr_notes')
def write_char(fp,char):
"""
Write the character to the file, catching any errors
"""
try:
fp.write(char)
except UnicodeDecodeError:
# skip any un-decodable characters for now
# see: http://wiki.python.org/moin/UnicodeDecodeError
pass
for i in range(1,52,1):
n = str(i).zfill(2)
url = url_stub.format(n)
data = urllib2.urlopen(url)
content = data.read()
title = 'CFR Title {0}'.format(n)
filepath = os.path.join(save_dir,'{0}.txt'.format(title))
print 'Writing: {0}'.format(title)
with codecs.open(filepath,encoding=ENCODING,mode='wb') as f:
for char in content:
write_char(f,char)
# The following is all specific to the Histrionics project
f.write('\n')
f.write(END_OF_TEXT + '\n')
f.write(u'title:{0}'.format(title))