473,322 Members | 1,347 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,322 software developers and data experts.

get wikipedia source failed (urrlib2)

Hi,
I'm trying to get wikipedia page source with urllib2:
usock = urllib2.urlopen("http://en.wikipedia.org/wiki/
Albert_Einstein")
data = usock.read();
usock.close();
return data
I got exception because HTTP 403 error. why? with my browser i can't
access it without any problem?

Thanks,
Shahar.

Aug 7 '07 #1
3 1464
On 7 , 11:54, shaha...@gmail.com wrote:
Hi,
I'm trying to get wikipedia page source with urllib2:
usock = urllib2.urlopen("http://en.wikipedia.org/wiki/
Albert_Einstein")
data = usock.read();
usock.close();
return data
I got exception because HTTP 403 error. why? with my browser i can't
access it without any problem?

Thanks,
Shahar.
This source works fine for other site. the problem is in wikipedia. is
someone now any solution for this problem?

Aug 7 '07 #2
<sh******@gmail.comwrote:
This source works fine for other site. the problem is in wikipedia. is
someone now any solution for this problem?
Wikipedia, AFAIK, bans requests without a User Agent.
http://www.voidspace.org.uk/python/a....shtml#headers

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Aug 7 '07 #3
In articleâ€* <‬11**********************@o61g2000hsh.googlegro ups.comâ€*>,‬
â€* ‬sh******@gmail.com wroteâ€*:‬

â€*‬Hiâ€*,‬
â€*‬I'm trying to get wikipedia page source with urllib2â€*:‬
â€* ‬usockâ€* = ‬urllib2â€*.‬urlopenâ€*("‬httpâ€*://‬en.wikipedia.org/wikiâ€*/‬
â€*‬Albert_Einsteinâ€*")‬
â€* ‬dataâ€* = ‬usock.readâ€*();‬
â€* ‬usock.closeâ€*();‬
â€* ‬return data
â€*‬I got exception because HTTP 403â€* ‬errorâ€*. ‬whyâ€*? ‬with my browser i can't
â€*‬access it without any problemâ€*?‬
â€*‬
â€*‬Thanksâ€*,‬
â€*‬Shaharâ€*.‬

It appears that Wikipedia may inspect the contents of the User-Agentâ€* ‬
HTTP headerâ€*, ‬and that it does not particularly like the string itâ€* ‬
receives from Python's urllibâ€*. ‬I was able to make it work with urllibâ€* ‬
via the following codeâ€*:‬

import urllib

class CustomURLopenerâ€* (‬urllib.FancyURLopenerâ€*):‬
â€* ‬versionâ€* = '‬Mozilla/5.0â€*'‬

urllibâ€*.‬_urlopenerâ€* = ‬CustomURLopenerâ€*()‬

uâ€* = ‬urllib.urlopenâ€*('‬httpâ€*://‬en.wikipedia.org/wiki/Albert_Einsteinâ€*')‬
dataâ€* = ‬u.readâ€*()‬

I'm assuming a similar trick could be used with urllib2â€*, ‬though I didn'tâ€* ‬
actually try itâ€*. ‬Another thing to watch out forâ€*, ‬is that some sitesâ€* ‬
will redirect a public URL X to an internal URL Yâ€*, ‬and will check thatâ€* ‬
access to Y is only permitted if the Referer field indicates coming fromâ€* ‬
somewhere internal to the siteâ€*. ‬I have seen both of these techniquesâ€* ‬
used to foil screen-scrapingâ€*.‬

Cheersâ€*,‬
â€*-‬M

â€*-- ‬
Michael Jâ€*. ‬Frombergerâ€* | ‬Lecturerâ€*, ‬Deptâ€*. ‬of Computer Science
httpâ€*://‬www.dartmouth.eduâ€*/‬~stingâ€*/ | ‬Dartmouth Collegeâ€*, ‬Hanoverâ€*, ‬NHâ€*, ‬USA
Aug 7 '07 #4

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

Similar topics

3
by: Claudio Grondi | last post by:
Is there an already available script/tool able to extract records and generate proper HTML code out of the data stored in the Wikipedia SQL data base? e.g. converting all occurences of ] to <a...
24
by: Roman | last post by:
Hello to everybody, I have some question concerning blocking view source from main menu of IE or MF. I am able to block right click with <body oncontextmenu="return false"> but still missing from...
24
by: Luis M. González | last post by:
For those interested in the simplest, easiest and most pythonic web framework out there, there's a new page in Wikipedia: http://en.wikipedia.org/wiki/Karrigell
9
by: AES | last post by:
I fairly often make PDF copies of web pages or sites by copying the web page link from the web page itself and pasting it into the Acrobat 7.0 Standard "Create PDF From Web Page" command. (Not...
48
by: spibou | last post by:
This concerns the Wikipedia article on C and C++ operators: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Until very recently the first table in the page was a very useful one on the...
3
by: lukefrancomusic | last post by:
I am trying to learn Python. I am working on a simple backup program (code listed below). When using a source directory (the files to be backed up) without spaces in the title, my program works...
27
by: John J. Lee | last post by:
I'm surprised to read this: http://en.wikipedia.org/wiki/Python_3 """Note that while there is no explicit requirement that code be able to run unmodified in both versions, in practice it is...
16
by: lovecreatesbea... | last post by:
It takes mu so time to finish this C source code line count function. What do you think about it? / ******************************************************************************* * Function ...
2
by: Taras_96 | last post by:
Hi everyone, I'm trying to run a number of commands stored within a sql file from within php using mysqli::query. The syntax I'm using is: source C:\data\projects\forum...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.