psx table
Browse files
app.py
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from selenium import webdriver
|
| 3 |
-
from selenium.webdriver.chrome.options import Options
|
| 4 |
-
from selenium.webdriver.common.by import By
|
| 5 |
-
from selenium.webdriver.support.ui import WebDriverWait
|
| 6 |
-
from selenium.webdriver.support import expected_conditions as EC
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
import time
|
| 9 |
import requests
|
|
@@ -14,61 +9,52 @@ app = FastAPI(
|
|
| 14 |
description="Scrape data from PSX website"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 24 |
|
| 25 |
-
# Initialize the driver
|
| 26 |
-
driver = webdriver.Chrome(options=chrome_options)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
soup = BeautifulSoup(page_source, 'html.parser')
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return results
|
| 66 |
-
|
| 67 |
-
except Exception as e:
|
| 68 |
-
print(f"Error: {e}")
|
| 69 |
-
return []
|
| 70 |
-
finally:
|
| 71 |
-
driver.quit()
|
| 72 |
|
| 73 |
# Usage example for the simple version
|
| 74 |
# dividend_info = simple_dividend_extraction('text.html')
|
|
@@ -82,7 +68,7 @@ def greet_json():
|
|
| 82 |
|
| 83 |
@app.get("/dividend_history")
|
| 84 |
def get_dividend():
|
| 85 |
-
return
|
| 86 |
|
| 87 |
|
| 88 |
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import time
|
| 4 |
import requests
|
|
|
|
| 9 |
description="Scrape data from PSX website"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
def simple_dividend_extraction():
|
| 13 |
+
"""
|
| 14 |
+
Simple function to extract dividend data
|
| 15 |
+
"""
|
| 16 |
+
r = requests.get('https://www.psx.com.pk/psx/announcement/financial-announcements')
|
| 17 |
+
print("request done")
|
|
|
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
| 21 |
+
table = soup.find('table')
|
| 22 |
+
_table = soup.select('list')
|
| 23 |
+
print(_table)
|
| 24 |
+
print(table)
|
| 25 |
+
|
| 26 |
+
results = []
|
| 27 |
+
|
| 28 |
+
if table:
|
| 29 |
+
print("in table")
|
|
|
|
| 30 |
|
| 31 |
+
#rows = table.find('tbody').findChildren('tr')
|
| 32 |
+
rows = table.find_all('tr')[1:]
|
| 33 |
+
print(f"Number of rows found: {len(rows)}")
|
| 34 |
|
| 35 |
+
for row in rows:
|
| 36 |
+
if not hasattr(row, 'find_all'):
|
| 37 |
+
print("no data")
|
| 38 |
+
continue
|
| 39 |
+
print(f"\nRow type: {type(row)}")
|
| 40 |
+
print(f"Row contents: {row}")
|
| 41 |
+
print("in row")
|
| 42 |
+
cells = row.find_all('td')
|
| 43 |
|
| 44 |
+
if len(cells) >= 6:
|
| 45 |
+
company_name = cells[1].get_text(strip=True)
|
| 46 |
+
dividend_amount = cells[2].get_text(strip=True) or "No dividend"
|
| 47 |
+
dividend_date = cells[5].get_text(strip=True) or "No date"
|
| 48 |
+
print("adding data")
|
| 49 |
+
|
| 50 |
+
results.append({
|
| 51 |
+
'Company': company_name,
|
| 52 |
+
'Dividend': dividend_amount,
|
| 53 |
+
'Date': dividend_date
|
| 54 |
+
})
|
| 55 |
+
print(results)
|
| 56 |
+
|
| 57 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Usage example for the simple version
|
| 60 |
# dividend_info = simple_dividend_extraction('text.html')
|
|
|
|
| 68 |
|
| 69 |
@app.get("/dividend_history")
|
| 70 |
def get_dividend():
|
| 71 |
+
return simple_dividend_extraction()
|
| 72 |
|
| 73 |
|
| 74 |
|