-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.py
24 lines (22 loc) · 881 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time
import datetime
from urllib.request import urlopen
from bs4 import BeautifulSoup
def generate_past_n_days(numdays, start_date):
"""Generate N days until now, e.g., [20151231, 20151230]."""
base = datetime.datetime.strptime(start_date, "%m/%d/%Y")
date_range = [base + datetime.timedelta(days=x) for x in range(0, numdays)]
return [x.strftime("%Y%m%d") for x in date_range]
def get_soup_with_repeat(url, repeat_times=3, verbose=True):
for i in range(repeat_times): # repeat in case of http failure
try:
time.sleep(np.random.poisson(3))
response = urlopen(url)
data = response.read().decode('utf-8')
return BeautifulSoup(data, "lxml")
except Exception as e:
if i == 0:
print(e)
if verbose:
print('retry...')
continue