-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscrape.py
More file actions
68 lines (57 loc) · 2.07 KB
/
Copy pathscrape.py
File metadata and controls
68 lines (57 loc) · 2.07 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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import csv
import os
from dotenv import load_dotenv
load_dotenv()
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver',options=chrome_options)
options = Options()
baseUrl=os.getenv('BASE_URL')
tailUrl=os.getenv('TAIL_URL')
def scrapeReport(filename):
url=filename+tailUrl
print(url)
driver.get(url)
content = driver.page_source
soup = BeautifulSoup(content,features='html.parser')
headtable=soup.find('table',id='container_statistics_head').find('tbody')
bodytable = soup.find('table',id='container_statistics_body')
headerrows=headtable.findAll('tr')
bodyrows = bodytable.findAll('tr')
try:
os.mkdir(os.getenv('OUTPUT_PATH'))
except OSError as e:
print('Dump Directory exists')
writeCsvFile(filename,headerrows,bodyrows)
def writeCsvFile(filename,headerrows,bodyrows):
with open(os.getenv('OUTPUT_PATH')+filename+'.csv', 'wt+', newline='') as f:
writer = csv.writer(f)
headers=[i for i in os.getenv('HEADERS').split(',')]
writer.writerow(headers)
for row in headerrows:
csv_row = []
csv_row.append(filename)
for cell in row.findAll(['td']):
csv_row.append(cell.text.strip())
writer.writerow(csv_row)
for row in bodyrows:
csv_row = []
csv_row.append(filename)
for cell in row.findAll(['td']):
s=cell.findAll('span')
if(s):
if(s[1].attrs.get('data-content')!=None):
csv_row.append(s[1].attrs.get('data-content'))
else:
s=cell.text.strip()
result=''.join([i for i in s if not i.isdigit()])
csv_row.append(result)
else:
csv_row.append(cell.text.strip())
writer.writerow(csv_row)
for name in os.listdir(os.getenv('REPORTS_PATH')):
if 'bahmniclinic' in name:
scrapeReport(name)