Home  >  Article  >  Backend Development  >  Why Am I Getting a \"403 Forbidden\" Error When Making Python Requests API Calls?

Why Am I Getting a \"403 Forbidden\" Error When Making Python Requests API Calls?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 03:19:30585browse

Why Am I Getting a

Troubleshooting "403 Forbidden" Errors in Python Requests API Calls

When attempting to extract data from a website using Python's requests module, it's not uncommon to encounter a "403 Forbidden" error. This error indicates that the server is refusing to fulfill the request due to access restrictions.

Consider the following code snippet:

<code class="python">import requests

url = 'http://worldagnetwork.com/'
result = requests.get(url)
print(result.content.decode())</code>

When executed, this code may return the following error:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

The issue in this case arises from the fact that the website rejects GET requests that lack a proper User-Agent header. By visiting the page in a browser (e.g., Chrome) and inspecting the network traffic, we can determine the User-Agent used by the browser.

To resolve the error, we need to add the User-Agent header to our requests call:

<code class="python">import requests

url = 'http://worldagnetwork.com/'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
result = requests.get(url, headers=headers)
print(result.content.decode())</code>

With the User-Agent header added, the request should now succeed in parsing the website's HTML content.

The above is the detailed content of Why Am I Getting a \"403 Forbidden\" Error When Making Python Requests API Calls?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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