I am trying to use requests to scrape data form NSE to find top gainers on Indian stock market. I have used this same method on yahoo finance and it worked but here, I keep getting the empty list as the result.
Here is my code:
import requests
from lxml import html
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}
resp = requests.get('https://www.nseindia.com/market-data/top-gainers-loosers', verify=False, headers=headers)
tree = html.fromstring(resp.content)
count = 1
stocks = []
for i in range(30):
name = tree.xpath('//*[@id="topgainer-Table"]/tbody/tr['+str(count)+']/td[1]/a')
print(name)
try:
stocks.append(name[0].text)
except:
pass
#name.text
count +=1
print(stocks)
As an output I get [ ] printed many times (an empty list). I think that the problem is with NSE because it has many different tables all with same xpaths.
Any ideas?