Susha from Russia
Susha from Russia

Reputation:

How can I change a user agent string programmatically?

I would like to write a program that changes my user agent string.

How can I do this in Python?

Upvotes: 5

Views: 8423

Answers (7)

Guest
Guest

Reputation: 11

As mentioned in the answers above, the user-agent field in the http request header can be changed using builtin modules in python such as urllib2. At the same time, it is also important to analyze what exactly the web server sees. A recent post on User agent detection gives a sample code and output, which gives a description of what the web server sees when a programmatic request is sent.

Upvotes: 1

nindalf
nindalf

Reputation: 1136

Updated for Python 3.2 (py3k):

import urllib.request
headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' }
url = 'http://www.google.com'
request = urllib.request.Request(url, b'', headers)
response = urllib.request.urlopen(request).read()

Upvotes: 0

ojrac
ojrac

Reputation: 13421

If you want to change the user agent string you send when opening web pages, google around for a Firefox plugin. ;) For example, I found this one. Or you could write a proxy server in Python, which changes all your requests independent of the browser.

My point is, changing the string is going to be the easy part; your first question should be, where do I need to change it? If you already know that (at the browser? proxy server? on the router between you and the web servers you're hitting?), we can probably be more helpful. Or, if you're just doing this inside a script, go with any of the urllib answers. ;)

Upvotes: 0

spiffyman
spiffyman

Reputation: 614

urllib2 is nice because it's built in, but I tend to use mechanize when I have the choice. It extends a lot of urllib2's functionality (though much of it has been added to python in recent years). Anyhow, if it's what you're using, here's an example from their docs on how you'd change the user-agent string:

import mechanize
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
opener.addheaders = [("User-agent", "Mozilla/5.0 (compatible; MyProgram/0.1)"),
                     ("From", "[email protected]")] 

Best of luck.

Upvotes: 2

Alex Martelli
Alex Martelli

Reputation: 881715

In urllib, it's done like this:

import urllib

class AppURLopener(urllib.FancyURLopener):
    version = "MyStrangeUserAgent"

urllib._urlopener = AppURLopener()

and then just use urllib.urlopen normally. In urllib2, use req = urllib2.Request(...) with a parameter of headers=somedict to set all the headers you want (including user agent) in the new request object req that you make, and urllib2.urlopen(req).

Other ways of sending HTTP requests have other ways of specifying headers, of course.

Upvotes: 3

Corey Goldberg
Corey Goldberg

Reputation: 60604

I assume you mean a user-agent string in an HTTP request? This is just an HTTP header that gets sent along with your request.

using Python's urllib2:

import urllib2

url = 'http://foo.com/'

# add a header to define a custon User-Agent
headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' }

req = urllib2.Request(url, '', headers)
response = urllib2.urlopen(req).read()

Upvotes: 9

Andre Miller
Andre Miller

Reputation: 15493

Using Python you can use urllib to download webpages and use the version value to change the user-agent.

There is a very good example on http://wolfprojects.altervista.org/changeua.php

Here is an example copied from that page:

>>> from urllib import FancyURLopener

>>> class MyOpener(FancyURLopener):
...   version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11)
 Gecko/20071127 Firefox/2.0.0.11'
>>> myopener = MyOpener()
>>> page = myopener.open('http://www.google.com/search?q=python')
>>> page.read()
[…]Results <b>1</b> - <b>10</b> of about <b>81,800,000</b> for <b>python</b>[…]

Upvotes: 2

Related Questions