Open URL - Python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your 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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.