-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswygx_holdings.py
executable file
·41 lines (30 loc) · 1.01 KB
/
swygx_holdings.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
#!/usr/bin/env python3
"""Store holdings of SWYGX."""
import pandas as pd
import common
class GetHoldingsError(Exception):
"""Error getting holdings."""
def save_holdings():
"""Writes SWYGX holdings to swygx_holdings DB table."""
with common.run_with_browser_page(
"https://www.schwabassetmanagement.com/allholdings/SWYGX"
) as page:
holdings = {}
common.schwab_browser_page(page)
for row in page.get_by_role("table").get_by_role("row").all()[1:]:
holdings[row.get_by_role("cell").nth(1).inner_text()] = float(
row.get_by_role("cell").nth(2).inner_text().strip("%")
)
if len(holdings) != 9:
print(f"Failed to get SWYGX holdings: only {len(holdings)} found: {holdings}")
raise GetHoldingsError
holdings_df = pd.DataFrame(
holdings,
index=[pd.Timestamp.now()],
)
common.to_sql(holdings_df, "swygx_holdings")
def main():
"""Main."""
save_holdings()
if __name__ == "__main__":
main()