user3662991 user3662991 - 1 year ago 219
Python Question

Changing User Agent in Python 3 for urrlib.request.urlopen

I want to open a url using


with urllib.request.urlopen('someurl') as url:
b =

I keep getting the following error:

urllib.error.HTTPError: HTTP Error 403: Forbidden

I understand the error to be due to the site not letting python access it, to stop bots wasting their network resources- which is understandable. I went searching and found that you need to change the user agent for urllib. However all the guides and solutions I have found for this issue as to how to change the user agent have been with urllib2, and I am using python 3 so all the solutions don't work.

How can I fix this problem with python 3?

Answer Source

From the Python docs:

import urllib.request
req = urllib.request.Request(
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'

f = urllib.request.urlopen(req)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download