close

[Solved] Python requests. 403 Forbidden

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python requests. 403 Forbidden in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python requests. 403 Forbidden Error Occurs?

Today I get the following error Python requests. 403 Forbidden in python.

How To Solve Python requests. 403 Forbidden Error ?

  1. How To Solve Python requests. 403 Forbidden Error ?

    To Solve Python requests. 403 Forbidden Error If you still get a 403 Forbidden after adding a user-agent, you may need to add more headers, such as referer:

  2. Python requests. 403 Forbidden

    To Solve Python requests. 403 Forbidden Error If you still get a 403 Forbidden after adding a user-agent, you may need to add more headers, such as referer:

Solution 1

It seems the page rejects GET requests that do not identify a User-Agent. I visited the page with a browser (Chrome) and copied the User-Agent header of the GET request (look in the Network tab of the developer tools):

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())

# <!doctype html>
# <!--[if lt IE 7 ]><html class="no-js ie ie6" lang="en"> <![endif]-->
# <!--[if IE 7 ]><html class="no-js ie ie7" lang="en"> <![endif]-->
# <!--[if IE 8 ]><html class="no-js ie ie8" lang="en"> <![endif]-->
# <!--[if (gte IE 9)|!(IE)]><!--><html class="no-js" lang="en"> <!--<![endif]-->
# ...

Solution 2

Just add to Alberto’s answer:

If you still get a 403 Forbidden after adding a user-agent, you may need to add more headers, such as referer:

headers = {
    'User-Agent': '...',
    'referer': 'https://...'
}

The headers can be found in the Network > Headers > Request Headers of the Developer Tools. (Press F12 to toggle it.)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read