get upcomming dividend
Browse files
app.py
CHANGED
|
@@ -8,40 +8,38 @@ app = FastAPI(
|
|
| 8 |
description="Scrape data from PSX website"
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
url = "https://www.psx.com.pk/psx/announcement/financial-announcements"
|
| 19 |
-
r = requests.get(url)
|
| 20 |
-
soup = BeautifulSoup(r.text, "html.parser")
|
| 21 |
|
|
|
|
| 22 |
table = soup.find("table")
|
|
|
|
| 23 |
if table is None:
|
| 24 |
-
|
| 25 |
|
| 26 |
-
rows = table.find_all("tr")
|
| 27 |
data = []
|
| 28 |
current_date = None
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
if not isinstance(row, Tag):
|
| 33 |
-
continue
|
| 34 |
|
| 35 |
-
#
|
| 36 |
header = row.find("h4")
|
| 37 |
if header:
|
| 38 |
current_date = header.get_text(strip=True)
|
| 39 |
continue
|
| 40 |
|
| 41 |
-
#
|
| 42 |
cols = row.find_all("td")
|
| 43 |
if len(cols) < 4:
|
| 44 |
-
continue
|
| 45 |
|
| 46 |
company = cols[0].get_text(strip=True)
|
| 47 |
dividend_amount = cols[3].get_text(strip=True)
|
|
@@ -53,3 +51,19 @@ def get_dividend():
|
|
| 53 |
})
|
| 54 |
|
| 55 |
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
description="Scrape data from PSX website"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def scrape_psx_dividends(url: str):
|
| 12 |
+
"""
|
| 13 |
+
Scrape company name, dividend date, and dividend amount
|
| 14 |
+
from PSX financial announcement table.
|
| 15 |
+
"""
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 20 |
table = soup.find("table")
|
| 21 |
+
|
| 22 |
if table is None:
|
| 23 |
+
raise ValueError("No <table> found on the page!")
|
| 24 |
|
|
|
|
| 25 |
data = []
|
| 26 |
current_date = None
|
| 27 |
|
| 28 |
+
# Loop through table rows
|
| 29 |
+
for row in table.find_all("tr"):
|
| 30 |
if not isinstance(row, Tag):
|
| 31 |
+
continue # Skip non-tag nodes
|
| 32 |
|
| 33 |
+
# Detect date header section
|
| 34 |
header = row.find("h4")
|
| 35 |
if header:
|
| 36 |
current_date = header.get_text(strip=True)
|
| 37 |
continue
|
| 38 |
|
| 39 |
+
# Extract columns from normal rows
|
| 40 |
cols = row.find_all("td")
|
| 41 |
if len(cols) < 4:
|
| 42 |
+
continue # Skip incomplete rows
|
| 43 |
|
| 44 |
company = cols[0].get_text(strip=True)
|
| 45 |
dividend_amount = cols[3].get_text(strip=True)
|
|
|
|
| 51 |
})
|
| 52 |
|
| 53 |
return data
|
| 54 |
+
|
| 55 |
+
@app.get("/hello")
|
| 56 |
+
def greet_json():
|
| 57 |
+
return {"Hello": "World!"}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@app.get("/dividend_history")
|
| 61 |
+
def get_dividend():
|
| 62 |
+
return scrape_psx_dividends(url='https://scstrade.com/MarketStatistics/MS_xDates.aspx')
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|