在BeautifulSoup.findAll函数中捕获异常

我正在尝试通过提取中的城市和区号来抓取此阿富汗页面table。现在,当我尝试刮擦此美国萨摩亚页面时,findAll()找不到<td>正确的页面。如何捕获此异常?


这是我的代码:


from bs4 import BeautifulSoup                                                                                                                                                                                                                

import urllib2                                                                                                                                                                                                                               

import re                                                                                                                                                                                                                                    


url = "http://www.howtocallabroad.com/american-samoa"

html_page = urllib2.urlopen(url)

soup = BeautifulSoup(html_page)


areatable = soup.find('table',{'id':'codes'})

d = {}


def chunks(l, n):

    return [l[i:i+n] for i in range(0, len(l), n)]


li = dict(chunks([i.text for i in areatable.findAll('td')], 2))

if li != []:

    print li


    for key in li:

            print key, ":", li[key]

else:

    print "list is empty"

这是我得到的错误


Traceback (most recent call last):

  File "extract_table.py", line 15, in <module>

    li = dict(chunks([i.text for i in areatable.findAll('td')], 2))

AttributeError: 'NoneType' object has no attribute 'findAll'

我也试过了,但是也没用


def gettdtag(tag):

    return "empty" if areatable.findAll(tag) is None else tag


all_td = gettdtag('td')

print all_td


守着一只汪
浏览 213回答 1
1回答

临摹微笑

错误说areatable是None:areatable = soup.find('table',{'id':'codes'})#areatable = soup.find('table', id='codes')&nbsp; # Also worksif areatable is None:&nbsp; &nbsp; print 'Something happened'&nbsp; &nbsp; # Exit out另外,我会用find_all代替findAll和get_text()代替text。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python