3

I want to extract tabular data based on given name and time period and save it to excel.
This is the link stock market info
Here is a snapshot
enter image description here There is an option in the webpage "Download file in csv format", but I want to automate this process, because I need some 100 companies data.

I tried the web query option in excel, but the above mentioned link doesn't show any tabular symbol for me to import.

I just want some pseudo code to begin with. Also is it possible in the first place?

Thanks in advance!

1 Answer 1

4

Hope this helps.

import requests
from bs4 import BeautifulSoup

symbol = "Company name"
from_date = "19-12-2016"
to_date = "20-12-2016"
URL = "https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?symbol=%s&segmentLink=3&symbolCount=3&series=ALL&dateRange=+&fromDate=%s&toDate=%s&dataType=PRICEVOLUMEDELIVERABLE" % (symbol,from_date,to_date)

r = requests.get(URL,verify=false)
bso = BeautifulSoup(r.text,'html.parser')

outfile = symobl+'_'+from_date+'_'+to_date+'.csv'
data = ''

for tag in bso.find_all('th'):
    data += tag.text.split()+','
data = data[:-1]+'\n'

for row in bso.find_all('tr')[1:]:
    for field in row.find_all('td'):
        data += field.text.split()+','
    data = data[:-1]+'\n'

with open(outfile,'w') as f:
    f.write(data)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy. I never thought it would be that easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.