-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrapper.py
66 lines (49 loc) · 1.62 KB
/
scrapper.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
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
import os
from datetime import datetime
import pandas as pd
BASE_URL = "https://www.zse.co.zw/price-sheet/"
def fetch_data_from_url(url):
"""
Fetch the market prices from the Zimbabwe Stock Exchange Website
"""
try:
response = pd.read_html(url, skiprows=1)
dataframe = response[0][3:]
# The response has 8 columns but only concerned with few
dataframe.columns = [
"Name",
"None",
"None",
"Opening_Price",
"Closing_Price",
"Volume_Traded",
]
# Lets filter the columns we are concerned with
df_trades = dataframe[
["Name", "Opening_Price", "Closing_Price", "Volume_Traded"]
]
# Drop all the columns with no data or missing all data
dataframe = df_trades.dropna(how="all").set_index("Name")
except Exception as e:
pass
return dataframe
def current_date():
"""
Generate the current date for use in saving the price sheet
"""
now = datetime.now()
current_date = now.strftime("%m-%d-%Y")
return current_date
def check_or_create_directory(dataframe, current_date):
"""
Create or save in the folder with other files
"""
os.makedirs("csv-daily-pricesheets", exist_ok=True)
# Save the data into both the CSV and Excel folders
dataframe.to_csv(f"csv-daily-pricesheets/{current_date}.csv")
dataframe.to_csv(f"xls-daily-price-sheets/{current_date}.xlsx")
def main():
dataframe = fetch_data_from_url(BASE_URL)
check_or_create_directory(dataframe, current_date())
if __name__ == "__main__":
main()