From 9408ddac6bf4842d5e3ba36b3f3eb104cfbe418a Mon Sep 17 00:00:00 2001 From: Sung Kim Date: Wed, 2 Dec 2015 20:50:50 +0800 Subject: [PATCH] Added requests.Session to make http keep-alive to improve the performance significantly --- bills/utils.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bills/utils.py b/bills/utils.py index 675f98d..97d267e 100755 --- a/bills/utils.py +++ b/bills/utils.py @@ -12,6 +12,8 @@ 'Referer': 'http://likms.assembly.go.kr/bill/jsp/BillSearchResult.jsp', } +s = requests.Session() + def check_dir(directory): if not os.path.exists(directory): os.makedirs(directory) @@ -25,7 +27,7 @@ def get_elem_texts(page, x): def get_webpage(url, outp): try: - r = requests.get(url, headers=HEADERS, stream=True) + r = s.get(url, headers=HEADERS, stream=True) assert r.ok except (requests.exceptions.RequestException, AssertionError) as e: import sys @@ -39,7 +41,7 @@ def get_webpage(url, outp): f.write(block) def get_webpage_text(url): - r = requests.get(url, headers=HEADERS) + r = s.get(url, headers=HEADERS) return r.content def read_json(fname): @@ -57,4 +59,4 @@ def read_webpage(filename): def write_json(data, fn): with open(fn, 'w') as f: json.dump(data, f, indent=2) - print 'Data written to ' + fn + #print 'Data written to ' + fn