Right, so I looked at some python tutorials about interfacing with internet, but every time a use a code such as:

import urllib
pagetext = urllib.urlopen("http://www.python.org.html").read()
print pagetext

It just comes up with a load of error messages.
how can i sort this

Recommended Answers

All 6 Replies

If you are using Python2 ...

import urllib
pagetext = urllib.urlopen("http://www.python.org").read()
print pagetext

For Python3 use ...

import urllib.request
pagetext = urllib.request.urlopen("http://www.python.org").read()
print(pagetext)

Using 2.5.4:
came up with

Traceback (most recent call last):
  File "C:/Python25/urltest.py", line 2, in <module>
    pagetext = urllib.urlopen("http://www.python.org").read()
  File "C:\Python25\Lib\urllib.py", line 82, in urlopen
    return opener.open(url)
  File "C:\Python25\Lib\urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "C:\Python25\Lib\urllib.py", line 325, in open_http
    h.endheaders()
  File "C:\Python25\lib\httplib.py", line 860, in endheaders
    self._send_output()
  File "C:\Python25\lib\httplib.py", line 732, in _send_output
    self.send(msg)
  File "C:\Python25\lib\httplib.py", line 699, in send
    self.connect()
  File "C:\Python25\lib\httplib.py", line 667, in connect
    socket.SOCK_STREAM):
IOError: [Errno socket error] (11001, 'getaddrinfo failed')

Strange ...

import urllib
pagetext = urllib.urlopen("http://www.python.org").read()
print pagetext

... works just fine with Python 2.5.4

You could try the improved version ...

import urllib2
pagetext = urllib2.urlopen("http://www.python.org").read()
print pagetext

Yep, no errors whatsoever with my Python 2.5.4 installation either...

No errors to me too (Vega's version)
Check your python installation

It could be the way 'Your_mum' is connected to the internet, maybe there is a filter or firewall somewhere. After all, 'www.Python.com' is a porn site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.