urllib2.HTTPError: HTTP Error 403: Forbidden while Downloading Historic Stock Data
When attempting to automate the download of historical stock data using Python, users may encounter an "urllib2.HTTPError: HTTP Error 403: Forbidden" error. This issue arises when accessing stock data from websites that restrict access based on headers or cookies.
Solution:
To resolve this issue, one must add the following set of headers to the request:
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'}
The 'Accept' header is particularly important, as it signifies the type of content that the client is prepared to accept. By specifying this header, the client demonstrates that it can handle HTML, XHTML, and XML data.
Once these headers are added to the request, the error should be resolved and the data can be successfully downloaded.
The above is the detailed content of Why Am I Getting a 'urllib2.HTTPError: HTTP Error 403: Forbidden' When Downloading Historical Stock Data?. For more information, please follow other related articles on the PHP Chinese website!