472,127 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Multiple Error Handling

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
0 947

Post your reply

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

Similar topics

2 posts views Thread by RJ | last post: by
3 posts views Thread by Disco-181 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.