Home > Backend Development > Python Tutorial > Why am I getting an HTTP Error 403: Forbidden when downloading stock data with urllib2?

Why am I getting an HTTP Error 403: Forbidden when downloading stock data with urllib2?

Susan Sarandon
Release: 2024-11-08 20:31:02
Original
1032 people have browsed it

Why am I getting an HTTP Error 403: Forbidden when downloading stock data with urllib2?

HTTP Error 403: Forbidden while Downloading Stock Data with urllib2

When attempting to automate the download of historical stock data using urllib2, users may encounter an HTTP Error 403: Forbidden. This error occurs when the server denies access to the requested resource due to insufficient permissions.

To resolve this issue, consider the following modifications to your code:

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'}
Copy after login

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

try:

page = urllib2.urlopen(req)
Copy after login

except urllib2.HTTPError, e:

print e.fp.read()
Copy after login

content = page.read()
print content

By adding the 'Accept' header, the code now specifies that it is capable of accepting various content types, including plain text, HTML, and XML. This allows the script to successfully negotiate with the server and retrieve the stock data.

The above is the detailed content of Why am I getting an HTTP Error 403: Forbidden when downloading stock data with urllib2?. 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