By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

urllib user agent?

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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michael <mo*****@mlug.missouri.edu> writes:
How do you change the user agent reported by urllib? I need to access
a resource that rejects anything but IE.


Look under _urlopener in the urllib documentation

http://www.python.org/doc/current/li...le-urllib.html

The example there seems to be just what you need...just change the
self.version line to something like

self.version = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)'

I hope this helps,

Tim
Jul 18 '05 #2

P: n/a
Michael <mo*****@mlug.missouri.edu> writes:
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.
Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.