Why Am I Getting a 'urllib2.HTTPError: HTTP Error 403: Forbidden' When Downloading Historical Stock Data?

Susan Sarandon
Release: 2024-11-11 10:15:02
Original
708 people have browsed it

Why Am I Getting a

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

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!

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