File size: 2,667 Bytes
6d2fdd8 ec315ba af8ef33 854f7dc ffba6f7 9c6d4b6 af8ef33 9c6d4b6 854f7dc ffba6f7 1dd03e1 77924e0 1dd03e1 109b35f 1dd03e1 77924e0 1dd03e1 77924e0 1dd03e1 f1067c7 4b85bce 1dd03e1 4b85bce 1dd03e1 77924e0 550eeeb 1dd03e1 550eeeb 6d2fdd8 58218b6 854f7dc 6d2fdd8 550eeeb 58218b6 550eeeb |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
from fastapi import FastAPI
from bs4 import BeautifulSoup
import time
import requests
from fastapi.responses import HTMLResponse
from starlette.responses import FileResponse
app = FastAPI(
title="PSX web scraper",
docs_url="/",
description="Scrape data from PSX website"
)
def simple_dividend_extraction():
"""
Simple function to extract dividend data
"""
r = requests.get('https://www.psx.com.pk/psx/announcement/financial-announcements')
print("request done")
soup = BeautifulSoup(r.text, 'html.parser')
table = soup.find('table')
_table = soup.select('list')
print(_table)
print(table)
results = []
if table:
print("in table")
#rows = table.find('tbody').findChildren('tr')
rows = table.find_all('tr')[1:]
print(f"Number of rows found: {len(rows)}")
for row in rows:
if not hasattr(row, 'find_all'):
print("no data")
continue
print(f"\nRow type: {type(row)}")
print(f"Row contents: {row}")
print("in row")
cells = row.find_all('td')
if len(cells) >= 6:
company_name = cells[0].get_text(strip=True)
dividend_amount = cells[3].get_text(strip=True) or "No dividend"
dividend_date = cells[8].get_text(strip=True) or "No date"
board_meeting = cells[7].get_text(strip=True) or "No meeting"
eps = cells[6].get_text(strip=True) or "No eps"
profit_loss_before_tax = cells[4].get_text(strip=True) or "No profit/loss"
profit_loss_after_tax = cells[5].get_text(strip=True) or "No profit/loss"
year_ended = cells[2].get_text(strip=True) or "No profit/loss"
results.append({
'Company': company_name,
'Dividend': dividend_amount,
'Date': dividend_date,
'BoardMeeting':board_meeting,
"Eps":eps,
'profitLossBeforeTax':profit_loss_before_tax,
'profitLossAfterTax':profit_loss_after_tax,
"yearEnded":year_ended
})
return results
@app.get("/hello")
def greet_json():
return {"Hello": "World!"}
@app.get("/dividend_history")
def get_dividend():
return simple_dividend_extraction()
@app.get("/PrivacyPolicy")
def get_privacy_policy():
return FileResponse('text.html')
@app.get("/portfolio")
def get_portfolio():
return FileResponse('portfolio.html')
|