Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web scrapping - Stopped working
#1
Hi, I wrote a small script as below

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

options = Options()
browser = webdriver.Chrome(executable_path=r"C:\Users\Admin\Downloads\chromedriver_win32\chromedriver.exe",options=options)
       
url = "https://www.nseindia.com/api/option-chain-equities?symbol=ACC"
    
browser.get(url)
soup = BeautifulSoup(browser.page_source,'lxml')
    
print(soup.prettify())
I got the url with query string (https://www.nseindia.com/api/option-chai...symbol=ACC) after doing Inspect and looking up the Network tab in developer tools.
The url that we are supposed to use via a browser is https://www.nseindia.com/option-chain. What I am trying to do is to read the json file with the values that the table in this page gets populated with.

This used to work and I was able to get the json file. But it seems to have stopped working and instead of an html page with the json, I am getting a message "Resource Not found"
If I copy the url from the Network tab via Inspect into a browser window, it displays the jason content. But if I put this url into my scipt, it gives the "Resource Not found" message.

Can you please help? Thank you
Reply
#2
It looks like when you visit https://www.nseindia.com/option-chain it sets up some cookies, that are used in a request header when requesting https://www.nseindia.com/api/option-chai...symbol=ACC , and without the cookie you get the
Quote:Resource not found

If you visit the site first then it should work (it does for me), try this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

options = Options()
browser = webdriver.Chrome(options =options)

url = "https://www.nseindia.com/api/option-chain-equities?symbol=ACC"

browser.get('https://www.nseindia.com/option-chain')
browser.get(url)

soup = BeautifulSoup(browser.page_source ,'lxml')

print(soup.prettify())
Reply
#3
Thanks mlieqo for your help
I tried your suggestion and it worked sometimes but not consistent.

I tried to do this in a loop with multiple stock symbols as below. Now I get a different error that "You are not authorized to access...". More over it does the loop once and for the second stock in the list, the target server is refusing connection even to https://www.nseindia.com/option-chain

I guess they are trying to prevent people from scrapping this site? Is my understanding correct, or am I doing something wrong?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import time

options = Options()
browser = webdriver.Chrome(executable_path=r"C:\Users\Admin\Downloads\chromedriver_win32\chromedriver.exe",options=options)
stocklist = ['ACC','HDFC','HCLTECH','ICICIBANK','RELIANCE','SBIN',] 

for symbl in stocklist:    
    url = "https://www.nseindia.com/api/option-chain-equities?symbol={stck}".format(stck=symbl)
    
    print('Trying nse option chain')
    browser.get('https://www.nseindia.com/option-chain')
    time.sleep(3)
    print('Symbol - {stck}'.format(stck=symbl))
    print('Trying api url')
    browser.get(url)   
    time.sleep(3)
    #soup = BeautifulSoup(browser.page_source,'lxml')
    browser.quit()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with scrapping Website giddyhead 1 1,631 Mar-08-2024, 08:20 AM
Last Post: AhanaSharma
  python web scrapping mg24 1 331 Mar-01-2024, 09:48 PM
Last Post: snippsat
  How can I ignore empty fields when scrapping never5000 0 1,395 Feb-11-2022, 09:19 AM
Last Post: never5000
  Suggestion request for scrapping html table Vkkindia 3 2,036 Dec-06-2021, 06:09 PM
Last Post: Larz60+
  web scrapping through Python Naheed 2 2,622 May-17-2021, 12:02 PM
Last Post: Naheed
  Website scrapping and download santoshrane 3 4,327 Apr-14-2021, 07:22 AM
Last Post: kashcode
  Image Scraper (beautifulsoup), stopped working, need to help see why woodmister 9 4,075 Jan-12-2021, 04:10 PM
Last Post: woodmister
  Newbie help with lxml scrapping chelsealoa 1 1,866 Jan-08-2021, 09:14 AM
Last Post: Larz60+
  Scrapping Sport score laplacea 1 2,259 Dec-13-2020, 04:09 PM
Last Post: Larz60+
  How to export to csv the output of every iteration when scrapping with a loop efthymios 2 2,294 Nov-30-2020, 07:46 PM
Last Post: efthymios

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020