Posted By

magicrebirth on 07/07/10


Tagged

url web urllib


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

saman


Open URL - Python


 / Published in: Python
 

URL: http://www.daniweb.com/forums/thread31148.html

  1. # if you are on the internet you can access the HTML code of a
  2. # given web site
  3. # using the urlopen() method/function from the module urllib2
  4.  
  5. import urllib2
  6.  
  7. urlStr = 'http://www.python.org/'
  8. try:
  9. fileHandle = urllib2.urlopen(urlStr)
  10. str1 = fileHandle.read()
  11. fileHandle.close()
  12. print '-'*50
  13. print 'HTML code of URL =', urlStr
  14. print '-'*50
  15. except IOError:
  16. print 'Cannot open URL %s for reading' % urlStr
  17. str1 = 'error!'
  18.  
  19. print str1

Report this snippet  

You need to login to post a comment.