with psx-data-reader, you can scrape the data of Pakistan stock exchange. psx-data-reader is super easy to use and handles everything for you. Just specify which company's stock data you want and how much you want, and the rest is done for you.
The psx-data-reader was written with fast use in mind. It provides the following key features
- can scrape all historical data till current date
- can scrape data for of multiple companies in a single line of code
- returns a
Pandas DataFramefor the scraped data - for better download speed, It does not request the complete data in a single network request rather it makes chunks of data to be downloaded and uses threads to open requests for different chunks of data, hence results in better speed
In the following paragraphs, I am going to describe how you can get and use Scrapeasy for your own projects.
To get psx-data-reader, either fork this github repo or simply use Pypi via pip.
$ pip install psx-data-readerFirst, import stocks and tickers from psx
from psx import stocks, tickers
to get the information of all the companies in Pakistan stock Exchange....
tickers = tickers()
to scrape the data of Silk Bank Limited we have pass its ticker (symbol) to the stocks method with proper start and end date. and it will return a DataFrame with the scraped data
data = stocks("SILK", start=datetime.date(2020, 1, 1), end=datetime.date.today())
we can also download the data of multiple companies in a single call to stocks method by passing a list or tuple of symbols
data = stocks(["SILK", "PACE"], start=datetime.date(2020, 1, 1), end=datetime.date.today())
and now the returned DataFrame object will have a hierarchical index on rows.
..venv\Scripts\Activate source .venv/bin/activate
python -m venv .venv python -m pip install --upgrade pip python -m pip install -e . python -m pip install -r requirements.txt
python .\src\psx\example.py
python src/psx/example.py
python .\src\psx\mongodb_example.py
python src/psx/mongodb_example.py
python -m psx.mongodb_example
This repo includes a Dockerfile and .dockerignore so you can deploy as a Railway service and attach a Schedule to run daily.
Steps:
- Push this repo to GitHub (with the provided
Dockerfile). - In Railway, create a new project → Deploy from GitHub → select this repo.
- In your service → Variables, set the following environment variables:
FINHISAAB_MONGO_URI=your-mongodb-uri
FINHISAAB_DB_NAME=finhisaab
FINHISAAB_COLLECTION=stockpricehistories
FINHISAAB_BATCH_SIZE=10
# Optional for testing a single batch
FINHISAAB_MAX_BATCHES=1
# Delays (seconds)
FINHISAAB_SYMBOL_DELAY_MIN=1
FINHISAAB_SYMBOL_DELAY_MAX=2
FINHISAAB_BATCH_DELAY_MIN=10
FINHISAAB_BATCH_DELAY_MAX=20
# Optional timezone for in-container date calculations (Dockerfile sets Asia/Karachi by default)
TZ=Asia/Karachi
- Add a Schedule in Railway (cron expression is in UTC). Example: run daily at 22:00 UTC (03:00 PKT):
0 22 * * *
The service’s default command (from the Dockerfile) executes:
python -u src/psx/mongodb_example.py
Logs are visible in Railway’s Deployments → Logs. The script performs an early MongoDB connectivity check and exits if unreachable.
mongodump --uri mongodb://192.168.0.131:27017/finhisaab --collection=stockpricehistories mongorestore --uri --nsInclude=finhisaab.stockpricehistories



