urllib user agent?

Discussion in 'Python' started by Michael, May 8, 2004.

  1. Michael

    Michael Guest

    How do you change the user agent reported by urllib? I need to access a
    resource that rejects anything but IE.
     
    Michael, May 8, 2004
    #1
    1. Advertising

  2. Michael

    Tim Heaney Guest

    Michael <> 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/lib/module-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
     
    Tim Heaney, May 9, 2004
    #2
    1. Advertising

  3. Michael

    Nelson Minar Guest

    Michael <> 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.
     
    Nelson Minar, May 9, 2004
    #3
  4. Michael

    f29 Guest

    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
     
    f29, May 9, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LIN

    User Agent

    LIN, Aug 14, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    479
    James J. Foster
    Aug 14, 2003
  2. Fuzzyman

    urllib - changing the user agent

    Fuzzyman, Jan 9, 2004, in forum: Python
    Replies:
    8
    Views:
    562
    Fuzzyman
    Jan 13, 2004
  3. Jonathan Gardner

    Asynchronous urllib (urllib+asyncore)?

    Jonathan Gardner, Feb 26, 2008, in forum: Python
    Replies:
    1
    Views:
    502
    Terry Jones
    Feb 27, 2008
  4. Chris McDonald
    Replies:
    0
    Views:
    326
    Chris McDonald
    Nov 1, 2010
  5. Luke Matuszewski
    Replies:
    8
    Views:
    670
    Luke Matuszewski
    Dec 2, 2005
Loading...

Share This Page