当 html 页面中包含文本 “内容逻辑定义”
的标记 h2
时,以下代码有效,例如 https://www.hl7.org/fhir/valueset-account-status.html
def extract_table(url):
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
h2 = soup.find(lambda elm: elm.name == 'h2' and 'Content Logical Definition' in elm.text)
div = h2.find_next_sibling('div')
return div.find('table')
但是对于以下不包含h2的网页,带有`“内容逻辑定义”,例如https://www.hl7.org/fhir/valueset-cpt-all.html返回以下错误:
'NoneType' object has no attribute 'find_next_sibling'
当网页中没有带有内容逻辑定义的h2
时,如何编辑代码以返回非表。
最佳答案
您可以通过两种常见方式来完成此操作:
-
h2 = soup.find(lambda elm: elm.name == 'h2' and 'Content Logical Definition' in elm.text) return div.find_next_sibling('div').find('table') if h2 else None
EAFP
- easier to ask for forgiveness than permission :try: h2 = soup.find(lambda elm: elm.name == 'h2' and 'Content Logical Definition' in elm.text) div = h2.find_next_sibling('div') return div.find('table') except AttributeError: return None
关于python - 编辑 BS4,当网页中没有表格时不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37643689/