user3662991 user3662991 - 1 year ago 451
Python Question

Changing user agent in python 3 for urrlib.urlopen?

So I'm somewhat new to python, and I want to open a url using urllib.request.urlopen('someurl'):

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

However I keep getting the following error

urllib.error.HTTPError: HTTP Error 403: Forbidden

which i understand is 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 ive 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.

So 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