urllib user agent?

M

Michael

How do you change the user agent reported by urllib? I need to access a
resource that rejects anything but IE.
 
N

Nelson Minar

Michael said:
How do you change the user agent reported by urllib? I need to access
a resource that rejects anything but IE.

Here's how I do it in urllib2:
req = urllib2.Request(url, None, {'User-agent': userAgent})
fp = urllib2.urlopen(req)

Beware, some earlier versions of Python had bugs in the urllib*
libraries where it may override your setting of user agent or other
HTTP headers. This works fine for me in Python 2.3.3.
 
F

f29

Use urllib2 instead:

import urllib2
req=urllib2.Request(url='http://targeturl',headers={'User-Agent':'Mozilla/5.0
(Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113'})

You may also attach another headers using this comfortable dictionary
interface.

rgrds,
f29
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top