403 Forbidden errors when request a webpage using Python requests

Error 

403 Forbidden errors when request a web page using Python requests. 


Cause

Usually, this is caused by the lack of headers indicating User-Agent. We can add the header information with User-Agent and send the request again.

import requests

url = 'http://example.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)
# Check the status code 
print(result.status_code)