damon
damon

Reputation: 8477

network unavailable from python script

I tried to access wikipedia page from python

a = urllib2.urlopen("http://en.wikipedia.org/wiki/LALR_parser")

this caused an error

<urlopen error [Errno 101] Network is unreachable>

So I tried

req = urllib2.Request(url, headers={'User-Agent' : "MyBrowser"})
a = urllib2.urlopen(req)

Still I get the same error

Now I am unable to view wikipedia in chrome or firefox..It says 'chrome cannot find the page'

But if I type in the wikipedia url in an anonymous proxy ,the page is displayed without any problem

What do you think is the problem?Is my IP blocked? I checked firewall(in ubuntu lucid)

sudo ufw status

Status: inactive

I also tried

sudo iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

can somebody please help?

Upvotes: 1

Views: 1180

Answers (3)

ylsun
ylsun

Reputation: 31

because your headers is not right ,use this to have a try :

import  urllib2
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1'}
req = urllib2.Request("http://en.wikipedia.org/wiki/LALR_parser", headers=headers)
a = urllib2.urlopen(req)
print a.read()

hope you good luck !

Upvotes: 1

ymn
ymn

Reputation: 2153

Are you using proxy? If you are using proxy, try to add following lines to your code:

import urllib2
proxy = urllib2.ProxyHandler({'http': 'user:password@your_proxy_server:proxy_port'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.python.org/')

Upvotes: 1

Moshe
Moshe

Reputation: 9899

Is it possible Wikipedia is blocking it? Running your supplied code raises an Exception:

urllib2.HTTPError: HTTP Error 403: Forbidden

It seems possible that Wikipedia might be blocking (simple) programmatic access to push people to use their API.

See Fetch a Wikipedia article with Python for more discussion about this problem.

Upvotes: 1

Related Questions