Made stuff into functions and cleaned up the code
This commit is contained in:
parent
f76b804f0c
commit
b8ef451357
38
parse.py
38
parse.py
@ -4,6 +4,8 @@ import requests
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
|
from multiprocessing import Process
|
||||||
|
|
||||||
# URLs to parse
|
# URLs to parse
|
||||||
modra_URL = "https://www.modra.si/skladi-in-podskladi/"
|
modra_URL = "https://www.modra.si/skladi-in-podskladi/"
|
||||||
infond_URL = "https://www.infond.si/tecajnica-vzajemnih-skladov"
|
infond_URL = "https://www.infond.si/tecajnica-vzajemnih-skladov"
|
||||||
@ -15,11 +17,9 @@ headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleW
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
# Parse Modra Zavarovalnica
|
# Parse Modra Zavarovalnica
|
||||||
#
|
#
|
||||||
|
def parse_modra():
|
||||||
a = requests.get(modra_URL, headers = headers)
|
a = requests.get(modra_URL, headers = headers)
|
||||||
|
df = pd.read_html(a.text, thousands=None)[0]
|
||||||
df_list = pd.read_html(a.text, thousands=None)
|
|
||||||
df = df_list[0]
|
|
||||||
|
|
||||||
# Rename Columns
|
# Rename Columns
|
||||||
df.rename(columns = {'VEP ? Vrednost enote premoženja':'VEP', 'Sklad':'SKLAD'}, inplace = True)
|
df.rename(columns = {'VEP ? Vrednost enote premoženja':'VEP', 'Sklad':'SKLAD'}, inplace = True)
|
||||||
@ -28,7 +28,8 @@ df.rename(columns = {'VEP ? Vrednost enote premoženja':'VEP', 'Sklad':'SKLAD'},
|
|||||||
df = df.filter(['SKLAD', 'VEP'])
|
df = df.filter(['SKLAD', 'VEP'])
|
||||||
|
|
||||||
# Drop all rows except the ones we want
|
# Drop all rows except the ones we want
|
||||||
subset = df[(df["SKLAD"]=="Dinamični podsklad") | (df["SKLAD"] =="Zajamčeni podsklad") ]
|
return df[(df["SKLAD"]=="Dinamični podsklad") | (df["SKLAD"] =="Zajamčeni podsklad") ]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -36,33 +37,30 @@ subset = df[(df["SKLAD"]=="Dinamični podsklad") | (df["SKLAD"] =="Zajamčeni po
|
|||||||
# Parse Sava Infond Skladi
|
# Parse Sava Infond Skladi
|
||||||
#
|
#
|
||||||
|
|
||||||
|
def parse_infond():
|
||||||
a = requests.get(infond_URL, headers = headers)
|
a = requests.get(infond_URL, headers = headers)
|
||||||
df_list = pd.read_html(a.text, thousands=None)
|
df = pd.read_html(a.text, thousands=None)[0]
|
||||||
|
|
||||||
|
|
||||||
# Drop all columns except the ones we want
|
# Drop all columns except the ones we want
|
||||||
df_list[0] = df_list[0].filter(['SKLAD', 'VEP'])
|
df = df.filter(['SKLAD', 'VEP'])
|
||||||
|
|
||||||
# Cleanup the "SKLAD" name
|
# Cleanup the "SKLAD" name
|
||||||
a = df_list[0].at[23,'SKLAD']
|
a = df.at[23,'SKLAD']
|
||||||
df_list[0].at[23,'SKLAD'] = a.split()[0]+' '+a.split()[1]
|
df.at[23,'SKLAD'] = a.split()[0]+' '+a.split()[1]
|
||||||
df_list[0].at[23,'VEP'] = a.split()[2]
|
df.at[23,'VEP'] = a.split()[2]
|
||||||
|
|
||||||
a = df_list[0].at[15,'SKLAD']
|
|
||||||
df_list[0].at[15,'SKLAD'] = a.split()[0]+' '+a.split()[1]
|
|
||||||
df_list[0].at[15,'VEP'] = a.split()[2]
|
|
||||||
|
|
||||||
|
a = df.at[15,'SKLAD']
|
||||||
|
df.at[15,'SKLAD'] = a.split()[0]+' '+a.split()[1]
|
||||||
|
df.at[15,'VEP'] = a.split()[2]
|
||||||
|
|
||||||
# Drop all rows except the ones we want
|
# Drop all rows except the ones we want
|
||||||
df = df_list[0]
|
return df[(df["SKLAD"]=="Infond Defensive") | (df["SKLAD"] =="Infond Technology") ]
|
||||||
subset1 = df[(df["SKLAD"]=="Infond Defensive") | (df["SKLAD"] =="Infond Technology") ]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Create new datatable and output it
|
# Create new datatable and output it
|
||||||
#
|
#
|
||||||
|
if __name__ == '__main__':
|
||||||
output_table = pd.concat([subset, subset1], axis=0)
|
output_table = pd.concat([parse_modra(), parse_infond()], axis=0)
|
||||||
output_table = output_table.reset_index(drop=True)
|
output_table = output_table.reset_index(drop=True)
|
||||||
print(output_table)
|
print(output_table)
|
||||||
|
Loading…
Reference in New Issue
Block a user