How to Fix 'urllib2.HTTPError: HTTP Error 403: Forbidden' When Downloading Historical Stock Data?

Susan Sarandon
Release: 2024-11-09 20:10:02
Original
1010 people have browsed it

How to Fix

HTTP Forbidden Error when Downloading Historical Stock Data with urllib2

Error:

"urllib2.HTTPError: HTTP Error 403: Forbidden"

Initial Attempts:

After encountering this error while trying to download historical stock data, several troubleshooting steps were taken, including:

  • Changing the user agent
  • Accepting response cookies

However, these attempts were unsuccessful.

Solution:

To resolve the error, additional HTTP headers were added to the request. The following code demonstrates the updated approach:

import urllib2,cookielib

site= "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=JPASSOCIAT&fromDate=1-JAN-2012&toDate=1-AUG-2012&datePeriod=unselected&hiddDwnld=true"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

req = urllib2.Request(site, headers=hdr)

try:
    page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
    print e.fp.read()

content = page.read()
print content
Copy after login

Explanation:

Adding the additional headers, particularly 'Accept', allowed the request to retrieve the CSV file without encountering the "HTTP Error 403: Forbidden" error.

The above is the detailed content of How to Fix 'urllib2.HTTPError: HTTP Error 403: Forbidden' When Downloading Historical Stock Data?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template