473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

urllib - changing the user agent

I'm writing a function that will query the comp.lang.python newsgroup
via google groups....... (I haven't got nntp access from work..)

I'm using urllib (for the first time)..... and google don't seem very
keen to let me search the group from within a program - the returned
pages all tell me 'you're not allowed to do that' :-)

I read in the urllib manual pages :

class URLopener( [proxies[, **x509]])

Base class for opening and reading URLs. Unless you need to support
opening objects using schemes other than http:, ftp:, gopher: or
file:, you probably want to use FancyURLopener.
By default, the URLopener class sends a User-Agent: header of
"urllib/VVV", where VVV is the urllib version number. Applications can
define their own User-Agent: header by subclassing URLopener or
FancyURLopener and setting the instance attribute version to an
appropriate string value before the open() method is called.
Could anyone tell me how to subclass this correctly with the version
attribute set and what text string I should use to mimic Internet
explorer and/or mozilla ?
Ta

Fuzzy
Jul 18 '05 #1
7 5798
mi*****@foord.net (Fuzzyman) writes:
[...]
I'm using urllib (for the first time)..... and google don't seem very
keen to let me search the group from within a program - the returned
pages all tell me 'you're not allowed to do that' :-)
Don't do it, then. Use the google API (mind you, not certain that
google groups is part of that).

[...] Could anyone tell me how to subclass this correctly with the version
attribute set and what text string I should use to mimic Internet
explorer and/or mozilla ?


IIRC,

opener.addheaders = [("User-agent", "whatever")]

Use a program like ethereal to find what your browser sends for
"whatever".
John
Jul 18 '05 #2
"Fuzzyman" <mi*****@foord.net> wrote in message
news:80**************************@posting.google.c om...
I'm writing a function that will query the comp.lang.python newsgroup
via google groups....... (I haven't got nntp access from work..)

I'm using urllib (for the first time)..... and google don't seem very
keen to let me search the group from within a program - the returned
pages all tell me 'you're not allowed to do that' :-)

I read in the urllib manual pages :

class URLopener( [proxies[, **x509]])

Base class for opening and reading URLs. Unless you need to support
opening objects using schemes other than http:, ftp:, gopher: or
file:, you probably want to use FancyURLopener.
By default, the URLopener class sends a User-Agent: header of
"urllib/VVV", where VVV is the urllib version number. Applications can
define their own User-Agent: header by subclassing URLopener or
FancyURLopener and setting the instance attribute version to an
appropriate string value before the open() method is called.
Could anyone tell me how to subclass this correctly with the version
attribute set and what text string I should use to mimic Internet
explorer and/or mozilla ?
Ta

Fuzzy


I normally use something like this for crawling webpages.

import urllib
urllib.URLopener.version = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT
5.0; T312461)'
urllib.FancyURLopener.prompt_user_passwd = lambda self, host, realm: (None,
None)

Anthony McDonald
Jul 18 '05 #3
On 9 Jan 2004 05:09:41 -0800, mi*****@foord.net (Fuzzyman) wrote:

[ wants to change the user-agent in HTTP request from urllib ]

Fuzzy --

I take the easy way out, and use urllib2, instead:

url = "http//www.spam.com/eggs.html"
req_headers = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
}
req = urllib2.Request(url, None, req_headers)
Jul 18 '05 #4
On 09 Jan 2004 14:12:56 +0000, jj*@pobox.com (John J. Lee) wrote:
Don't do it, then. Use the google API (mind you, not certain that
google groups is part of that).


Can Google APIs be used to access Google Groups? ...

No. The Google Web APIs service can only be used to search
Google's main index of 3 billion web pages.

http://www.google.com/apis/api_faq.html#ini2

Jul 18 '05 #5
Terry Carroll <ca*****@tjc.com> writes:
On 9 Jan 2004 05:09:41 -0800, mi*****@foord.net (Fuzzyman) wrote:

[ wants to change the user-agent in HTTP request from urllib ]

Fuzzy --

I take the easy way out, and use urllib2, instead: [...] req = urllib2.Request(url, None, req_headers)


or again, you can set .addheaders on OpenerDirector (which will cause
those headers to be added to all requests).
John
Jul 18 '05 #6
|Thus Spake John J. Lee On the now historical date of Fri, 09 Jan 2004
20:16:54 +0000|
or again, you can set .addheaders on OpenerDirector (which will cause
those headers to be added to all requests).


This, however, does not stop the original User-agent header to be sent,
and google still filters out the request. Instead, it just causes a
second user-agent to be sent.

Here is the very bad code I used to solve this problem. There are better
ways, I assure you, but it should point you in the right direction. You
should probably do this with inheritance, but for my quick script, this is
what I did.

----
#Override the default OpenerDirector Class Init.
#OpenerDirector *insists* on adding a User-Agent
#That some websites don't like.
foo = OpenerDirector.__init__
def bar(self, Agent='None'):
foo(self)
self.addheaders = [('User-Agent',Agent)]

OpenerDirector.__init__ = bar
-----

HTH

Sam Walters.

--
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
Do you really want to go there?"""

Jul 18 '05 #7
Samuel Walters <sw*************@yahoo.com> writes:
|Thus Spake John J. Lee On the now historical date of Fri, 09 Jan 2004
20:16:54 +0000|
or again, you can set .addheaders on OpenerDirector (which will cause
those headers to be added to all requests).

(just to clarify, I meant an OpenerDirector instance, not the class
itself)

This, however, does not stop the original User-agent header to be sent,
and google still filters out the request. Instead, it just causes a
second user-agent to be sent.
No, it does stop them being sent. Perhaps you mutated the base class
..addheaders by mistake (using .append(("User-agent", "blah")), for
example)? Don't do that! Mutating class attributes is a bad idea.

Here is the very bad code I used to solve this problem. There are better
ways, I assure you, but it should point you in the right direction. You

[...]

No need to assure me of *that* <wink>. You can call a base class
constructor directly, you know. And clobbering the base class'
constructor is another very bad idea.
John
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael | last post by:
How do you change the user agent reported by urllib? I need to access a resource that rejects anything but IE.
11
by: Pater Maximus | last post by:
I am trying to implement the recipe listed at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/211886 However, I can not get to first base. When I try to run import urllib...
0
by: Chris | last post by:
hello, I have an odd behaviour. I try to download some files connected to a specific webpage (e.g. all stylesheets) with urllib2.urlopen(x) This seems to hang on the 2nd file or so. Doing the...
8
by: Gabriel Zachmann | last post by:
Here is a very simple Python script utilizing urllib: import urllib url = "http://commons.wikimedia.org/wiki/Commons:Featured_pictures/chronological" print url print file = urllib.urlopen(...
1
by: evanpmeth | last post by:
I have tried multiple ways of posting information to a website and have failed. I have seen this problem on other forums can someone explain or point me to information on how POST works through...
4
by: junkdump2861 | last post by:
Here's the problem: using Netscape 7.1, I type use the view page source command (url is http://en.wikipedia.org/wiki/Cain) and save the raw HTML file and it's 67 kb, and has the addresses of all...
5
by: Adrian Smith | last post by:
I'm trying to use urllib2 to download a page (I'd rather use urllib, but I need to change the User-Agent header to look like a browser or G**gle won't send it to me, the big meanies). The following...
0
by: johnpollard | last post by:
For some reason this script isn't working and I dont know what it is. I believe the problem lies in the following lines of code since the script works with a different website and username/password...
5
by: Thierry | last post by:
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.