473,373 Members | 1,415 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,373 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 1465
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. Gonzlez | 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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.