954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python and the internet

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

Your_mum
Light Poster
30 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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)
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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')
Your_mum
Light Poster
30 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

shadwickman
Posting Pro in Training
497 posts since Jul 2007
Reputation Points: 186
Solved Threads: 77
 

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You