简体   繁体   中英

how to apply “catch-all” exception clause to complex python web-scraping script?

I've got a list of 100 websites in CSV format. All of the sites have the same general format, including a large table with 7 columns. I wrote this script to extract the data from the 7th column of each of the websites and then write this data to file. The script below partially works, however: opening the output file (after running the script) shows that something is being skipped because it only shows 98 writes (clearly the script also registers a number of exceptions). Guidance on how to implement a "catching exception" in this context would be much appreciated. Thank you!

import csv, urllib2, re
def replace(variab): return variab.replace(",", " ")

urls = csv.reader(open('input100.txt', 'rb'))  #access list of 100 URLs
for url in urls:
    html = urllib2.urlopen(url[0]).read()  #get HTML starting with the first URL
    col7 = re.findall('td7.*?td', html)  #use regex to get data from column 7
    string = str(col7)  #stringify data
    neat = re.findall('div3.*?div', string)  #use regex to get target text  
    result = map(replace, neat)  #apply function to remove','s from elements
    string2 = ", ".join(result)  #separate list elements with ', ' for export to csv
    output = open('output.csv', 'ab') #open file for writing 
    output.write(string2 + '\n') #append output to file and create new line
    output.close()

Return:

Traceback (most recent call last):
 File "C:\Python26\supertest3.py", line 6, in <module>
  html = urllib2.urlopen(url[0]).read()
 File "C:\Python26\lib\urllib2.py", line 124, in urlopen
  return _opener.open(url, data, timeout)
 File "C:\Python26\lib\urllib2.py", line 383, in open
  response = self._open(req, data)
 File "C:\Python26\lib\urllib2.py", line 401, in _open
  '_open', req)
 File "C:\Python26\lib\urllib2.py", line 361, in _call_chain
  result = func(*args)
 File "C:\Python26\lib\urllib2.py", line 1130, in http_open
  return self.do_open(httplib.HTTPConnection, req)
 File "C:\Python26\lib\urllib2.py", line 1103, in do_open
  r = h.getresponse()
 File "C:\Python26\lib\httplib.py", line 950, in getresponse
  response.begin()
 File "C:\Python26\lib\httplib.py", line 390, in begin
  version, status, reason = self._read_status()
 File "C:\Python26\lib\httplib.py", line 354, in _read_status
  raise BadStatusLine(line)
BadStatusLine
>>>>

Make the body of your for loop into:

for url in urls:
  try:
    ...the body you have now...
  except Exception, e:
    print>>sys.stderr, "Url %r not processed: error (%s) % (url, e)

(Or, use logging.error instead of the goofy print>> , if you're already using the logging module of the standard library [and you should;-)]).

我建议阅读Errors and Exceptions Python文档,尤其是第8.3节-处理异常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM