Repository navigation
Expand file tree
/
Copy pathdownload.py
More file actions
67 lines (55 loc) · 1.88 KB
/
Copy pathdownload.py
File metadata and controls
67 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
Downloads all the XPT files in the specified URL, and stores them in the
destination folder. Two arguments are mandatory: first: the desired NHANES cycle,
and second, the destination folder.
@copyright: Fathom Information Design 2014
'''
import sys
import os.path
from bs4 import BeautifulSoup
import requests
import urllib
def load_components():
ifile = open('components', 'r')
components = []
for line in ifile.readlines():
line = line.strip()
if line == "" or line[0] == "#": continue
parts = line.split()
if len(parts) == 2:
comp_name = parts[0]
components.append(comp_name)
ifile.close()
return components
def request_file(src_url, dest_fn):
try:
r = requests.get(src_url, stream = True)
with open(dest_fn, 'wb') as f:
for ch in r:
f.write(ch)
except:
print(" Failed downloading file", src_url, ", canceling!")
sys.exit()
components = load_components()
cycle = sys.argv[1]
dest_dir = sys.argv[2]
begin_year = cycle.split('-')[0]
print("Getting XPT files for cycle " + cycle + "...")
base_url = "https://wwwn.cdc.gov"
url_template = "https://wwwn.cdc.gov/nchs/nhanes/Search/DataPage.aspx?Component=${comp}&CycleBeginYear=${year}"
for comp in components:
url = url_template.replace("${comp}", comp).replace("${year}", begin_year)
print(comp, "component")
content = urllib.request.urlopen(url).read()
soup = BeautifulSoup(content, features="lxml")
table = soup.find( "table")
for a in table.find_all("a", href=True):
url = a["href"]
name = os.path.split(url)[1]
ext = os.path.splitext(name)[1].lower()
if ext == ".xpt":
dest_fn = os.path.join(dest_dir, name)
full_url = base_url + url
print(" ", full_url, "...", dest_fn)
request_file(full_url, dest_fn)
print("Done.")