擅长:python、mysql、java
<p>可以指定一个默认值,以便在没有元素匹配的情况下返回。
比如:</p>
<pre><code>def html_check(td, text):
if td.text == text:
value = td.find_next('td').text
return value
return "Default Value"
</code></pre>
<p>此外,您还可以通过参数指定默认值,这有点像:</p>
^{pr2}$
<p>然后使用它,就像:</p>
<pre><code>for tr in table_body.find_all('tr'):
for td in tr:
description= html_check(td, 'Description', 'Default Description')
category = html_check(td, 'Category','Default Category')
department = html_check(td, 'Department', 'Default Department')
justification = html_check(td, 'Justification', 'Default Justification')
print(description, category, department, justification)
</code></pre>