构建数据库的缩进级别错误

时间:2014-03-30 04:48:38

标签: python syntax-error

我从情绪分析教程中得到了这段代码......但是这段代码出了什么问题......这里出错了

def huffingtonRSSvisit():
    try:
        page = 'http://feeds.huffingtonpost.com/huffingtonpost/raw_feed'
        sourceCode = opener.open(page) .read()
        try:
            links = re.findall(r'<link.*href=\"(.*?)\"', sourceCode)
            for link in links:
                if '.rdf' in link:
                    pass
                else:
                    print 'visiting the link'
                    print '########################'
                    linkSource = opener.open(link) .read()
                    linesOfInterest = re.findall(r'<p>(.*?)</p>',str(linkSource))
                    print 'Content:'
                    for eachLine in linesOfInterest:
                        if '<img width' in eachLine:
                            pass
                        elif '<a href=' in eachLine:
                            pass
                        else:
                            print eachLine



                        time.sleep(5)

         except Exception, e:
         print 'failed main loop of huffingtonRSS'
         print str(e)
  

文件&#34;&#34;,第28行       除了例外,e:^   IndentationError:unindent与任何外部缩进级别都不匹配

1 个答案:

答案 0 :(得分:0)

每次尝试:block都需要与exception子句匹配。由于您的代码有两个try:行,因此需要两个异常块:

def huffingtonRSSvisit():
    try:
        page = 'http://feeds.huffingtonpost.com/huffingtonpost/raw_feed'
        sourceCode = opener.open(page) .read()
        try:
            links = re.findall(r'<link.*href=\"(.*?)\"', sourceCode)
            for link in links:
                if '.rdf' in link:
                    pass
                else:
                    print 'visiting the link'
                    print '########################'
                    linkSource = opener.open(link) .read()
                    linesOfInterest = re.findall(r'<p>(.*?)</p>',str(linkSource))
                    print 'Content:'
                    for eachLine in linesOfInterest:
                        if '<img width' in eachLine:
                            pass
                        elif '<a href=' in eachLine:
                            pass
                        else:
                            print eachLine    
                        time.sleep(5)
         except Exception, e:
             print 'failed inner try block of huffingtonRSS'
             print str(e)
     except Exception, e:
         print 'failed outer try block of huffingtonRSS'
         print str(e)