[英]Beautiful Soup - handling errors
我想知道如何处理<strong>Text:</strong>
后href
不存在的情况
有没有更好的方法来搜索<strong>Contact:</strong>
之后存在的内容
findNext怎么样?
import re
from BeautifulSoup import BeautifulSoup
html = '''<strong>Text:</strong>
<a href='http://domain.com'>url</a>'''
soup = BeautifulSoup(html)
label = soup.find("strong" , text='Text:')
contact = label.findNext('a')
if contact.get('href') != None:
print contact
else:
print "No href"
如果您正在寻找具有href
a
标签,请使用:
contact = label.findNext('a', attrs={'href' : True})
有了这个,你不需要压缩空格。 我想你是这样做的,因为next
是在标签后面返回空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.