How to change user agent program line?

I would like to write a program that changes the line of my user agent.

How can I do this in Python?

+4
source share
7 answers

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

using Python 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() 
+8
source

In urllib this is done as follows:

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

and then just use urllib.urlopen as usual. In urllib2 use req = urllib2.Request(...) with the headers=somedict parameter to set all the necessary headers (including the user agent) to the new req request object, and urllib2.urlopen(req) .

Other ways to send HTTP requests, of course, have other ways to specify headers.

+3
source

Using Python, you can use urllib to load web pages and use the version value to change the user agent.

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

Here is an example copied from this 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>[…] 
+2
source

urllib2 is good because it is built-in, but I prefer to use mechanize when I have a choice. It extends the functionality of urllib2 (although most of it has been added to python in recent years). Anyway, if this is what you are using, here is an example from their docs on how you 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", " responsible.person@example.com ")] 

Good luck.

+2
source

As mentioned in the answers above, the user-agent field in the header of the HTTP request can be changed using built-in modules in python such as urllib2. At the same time, it is also important to analyze what exactly the web server sees. A recent publication on the discovery of a user agent provides sample code and output that provides a description of what the web server sees when a program request is sent.

+1
source

If you want to change the user agent string that you send when you open web pages, google for the Firefox plugin .;) For example, I found this one . Or you can write a proxy server in Python that modifies all your requests regardless of browser.

My point is that changing the string will be the easy part; Your first question should be, where do I need to change it? If you already know that (on the browser proxy? Are you banging on the router between you and the web servers?), We can probably be more useful. Or, if you just do it inside a script, go to any of urllib answers urllib )

0
source

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() 
0
source

Source: https://habr.com/ru/post/1286143/


All Articles