By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

Multiple Error Handling

P: 7
Hi,

I am trying to input a spreadsheet of possible domain names and output the length of the sourcecode of the webpage (if it exists). In testing this, I’ve come across a lot of errors, for example, when websites ask for username/password, take too slow to load, or no longer work. I am a newbie and am looking for help dealing with multiple errors.

First, is the basic code setup below the best way to deal with multiple errors? I would like to have the program read the lines if possible, and return back null (or string=’’) if something goes wrong; do I have to do this ad hoc, adding new errors to my list each time I found one, or is there some way to say ‘return null if any error comes up?’

Second, how do I handle this error?

InvalidURL: nonnumeric port: ''

It seems to happen on z=urlopen('http://www.'+domainTerm)

Third, how do I handle this error?

error: (10054, 'Connection reset by peer')

It seems to happen on lineList=z.readlines()

Thank you,
Mitch
Expand|Select|Wrap|Line Numbers
  1. from mechanize import Browser
  2. from urllib import urlopen
  3. import re, time, urllib2, string
  4.  
  5. f = open('rawST2.csv','r')
  6. lines = f.readlines()
  7. f.close()
  8. f2 = open('toy4.csv','w')
  9. f2.write(lines[0].rstrip()+',sourcelines'+',sourcecharacters' +"\n")
  10. print(lines[0].rstrip()+",sourcelines"+",sourcecharacters)
  11.  
  12. domainTerms=[]
  13. for i in range(1,len(lines)):
  14.     domainTerms.append( lines[i].split(',')[5].rstrip() )
  15.  
  16. x = domainTerms[59]
  17. domainTerm=string.replace(x,x[0],"")
  18.  
  19. try: 
  20.   z=urlopen('http://www.'+domainTerm)
  21.   lineList=z.readlines()
  22. except (urllib2.URLError, ValueError, IOError, AttributeError, TypeError):
  23.   lineList=""
  24.  
---
Aug 19 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.