H.C*_*hoi 90 python exception-handling list
我正在使用BeautifulSoup并解析一些HTML.
我从每个HTML (使用for循环)获取某些数据并将该数据添加到某个列表中.
问题是,一些HTML具有不同的格式(并且它们没有我想要的数据).
所以,我试图使用异常处理并null为列表添加值(我应该这样做,因为数据序列很重要.)
例如,我有一个代码,如:
soup = BeautifulSoup(links)
dlist = soup.findAll('dd', 'title')
# I'm trying to find content between <dd class='title'> and </dd>
gotdata = dlist[1]
# and what i want is the 2nd content of those
newlist.append(gotdata)
# and I add that to a newlist
Run Code Online (Sandbox Code Playgroud)
而且有些链接没有<dd class='title'>,所以我想做的是将字符串添加null到列表中.
出现错误:
list index out of range.
Run Code Online (Sandbox Code Playgroud)
我尝试过的是添加这样的一些行:
if not dlist[1]:
newlist.append('null')
continue
Run Code Online (Sandbox Code Playgroud)
但它没有成功.它仍然显示错误:
list index out of range.
Run Code Online (Sandbox Code Playgroud)
我该怎么办?我应该使用异常处理吗?或者有更简单的方法吗?
有什么建议?任何帮助都会非常棒!
Thi*_*ter 215
处理异常是要走的路:
try:
gotdata = dlist[1]
except IndexError:
gotdata = 'null'
Run Code Online (Sandbox Code Playgroud)
当然,你也可以检查len()的dlist; 但处理异常更直观.
Mar*_*ers 27
你有两个选择; 处理异常或测试长度:
if len(dlist) > 1:
newlist.append(dlist[1])
continue
Run Code Online (Sandbox Code Playgroud)
要么
try:
newlist.append(dlist[1])
except IndexError:
pass
continue
Run Code Online (Sandbox Code Playgroud)
如果经常没有第二项,则使用第一项;如果有时没有第二项,则使用第二项.
Rya*_*ing 20
三元就足够了.更改:
gotdata = dlist[1]
Run Code Online (Sandbox Code Playgroud)
至
gotdata = dlist[1] if len(dlist) > 1 else 'null'
Run Code Online (Sandbox Code Playgroud)
这是一个简短的手
if len(dlist) > 1:
gotdata = dlist[1]
else:
gotdata = 'null'
Run Code Online (Sandbox Code Playgroud)
小智 5
对于任何对更短的方式感兴趣的人:
gotdata = len(dlist)>1 and dlist[1] or 'null'
Run Code Online (Sandbox Code Playgroud)
但为了获得最佳性能,我建议使用False而不是'null',那么一行测试就足够了:
gotdata = len(dlist)>1 and dlist[1]
Run Code Online (Sandbox Code Playgroud)