diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2019-10-27 16:25:32 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2019-10-27 18:00:05 +0100 |
commit | 0915b55c80a1bb328f3a1044e34934bf6b5fa04e (patch) | |
tree | bb72c8878c6d6a4f2d20904d432afb8e8c6ac9d5 /update/at_postleitzahl.py | |
parent | 40961fc0a014c72c4981d3878b886f19ec3f2f9a (diff) |
Switch update scripts to use requests
This makes the scripts more consistent.
Diffstat (limited to 'update/at_postleitzahl.py')
-rwxr-xr-x | update/at_postleitzahl.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/update/at_postleitzahl.py b/update/at_postleitzahl.py index 89308b7..596f5d0 100755 --- a/update/at_postleitzahl.py +++ b/update/at_postleitzahl.py @@ -26,9 +26,9 @@ from __future__ import print_function, unicode_literals import os import os.path -import urllib import lxml.html +import requests import xlrd @@ -58,8 +58,9 @@ regions = { def find_download_url(): """Extract the spreadsheet URL from the Austrian Post website.""" - f = urllib.urlopen(base_url) - document = lxml.html.parse(f) + response = requests.get(base_url) + response.raise_for_status() + document = lxml.html.document_fromstring(response.content) url = [ a.get('href') for a in document.findall('.//a[@href]') @@ -69,9 +70,10 @@ def find_download_url(): def get_postal_codes(download_url): """Download the Austrian postal codes spreadsheet.""" - content = urllib.urlopen(download_url).read() + response = requests.get(download_url) + response.raise_for_status() workbook = xlrd.open_workbook( - file_contents=content, logfile=open(os.devnull, 'w')) + file_contents=response.content, logfile=open(os.devnull, 'w')) sheet = workbook.sheet_by_index(0) rows = sheet.get_rows() # the first row contains the column names @@ -92,7 +94,7 @@ if __name__ == '__main__': # download/parse the information download_url = find_download_url() # print header - print('# generated from %s downloaded from ' % + print('# generated from %s downloaded from' % os.path.basename(download_url)) print('# %s' % base_url) # build an ordered list of postal codes |