bs4 列表理解 'if' 语句
bs4 list comprehension with 'if' statement
如果 table 行包含文本,我将尝试将每个 table 行的文本添加到我的列表中。我想使用列表理解来做到这一点。
这是我试过的
listt2 = [s.span.text for s in soup.find_all('tr') if s.span.text]
这是错误
listt2 = [s.span.text for s in soup.find_all('tr') if s.span.text]
AttributeError: 'NoneType' object has no attribute 'text'
这里有 1 个 'tr' 包含 span 标签:
<tr>
<td colspan="2" class="cell--section-end cell--link cell--link__icon">
<a data-analytics="[Competitions] - German Bundesliga" href="/football/german-bundesliga/event/26301018" class="cell--link__link cell-text">
<i class="i accordion__title-icon--green accordion__title-icon--right" data-char=""></i> <b class="cell-text__line cell-text__line--icon">
<span class="competitions-team-name js-ev-desc">1. FC Köln v 1899 Hoffenheim</span>
</b>
</a>
</td>
<tr>
这是另一个没有的:
<tr>
<td colspan="5" class="group-header">
Sat 14:30 </td>
</tr>
请注意此页面上还有更多 tr 标签
如果您只想获取包含 <span>
标签的 <tr>
个标签,您可以使用此列表理解:
listt2 = [s.span.text for s in soup.select('tr:has(span)') if s.span.text]
编辑:
from bs4 import BeautifulSoup
html_doc = '''<tr>
<td colspan="2" class="cell--section-end cell--link cell--link__icon">
<a data-analytics="[Competitions] - German Bundesliga" href="/football/german-bundesliga/event/26301018" class="cell--link__link cell-text">
<i class="i accordion__title-icon--green accordion__title-icon--right" data-char=""></i> <b class="cell-text__line cell-text__line--icon">
<span class="competitions-team-name js-ev-desc">1. FC Köln v 1899 Hoffenheim</span>
</b>
</a>
</td>
<tr>'''
soup = BeautifulSoup(html_doc, 'html.parser')
listt2 = [s.span.text for s in soup.select('tr:has(span)') if s.span.text]
print(listt2)
打印:
['1. FC Köln v 1899 Hoffenheim']
您只需在查找 span.text
之前检查 span is not None
。
listt2 = [s.span.text for s in soup.find_all('tr') if s.span is not None and s.span.text]
因为 short-circuiting,如果 s.span is None
则永远不会评估 s.span.text
因为 False and *
是 False
如果 table 行包含文本,我将尝试将每个 table 行的文本添加到我的列表中。我想使用列表理解来做到这一点。
这是我试过的
listt2 = [s.span.text for s in soup.find_all('tr') if s.span.text]
这是错误
listt2 = [s.span.text for s in soup.find_all('tr') if s.span.text]
AttributeError: 'NoneType' object has no attribute 'text'
这里有 1 个 'tr' 包含 span 标签:
<tr>
<td colspan="2" class="cell--section-end cell--link cell--link__icon">
<a data-analytics="[Competitions] - German Bundesliga" href="/football/german-bundesliga/event/26301018" class="cell--link__link cell-text">
<i class="i accordion__title-icon--green accordion__title-icon--right" data-char=""></i> <b class="cell-text__line cell-text__line--icon">
<span class="competitions-team-name js-ev-desc">1. FC Köln v 1899 Hoffenheim</span>
</b>
</a>
</td>
<tr>
这是另一个没有的:
<tr>
<td colspan="5" class="group-header">
Sat 14:30 </td>
</tr>
请注意此页面上还有更多 tr 标签
如果您只想获取包含 <span>
标签的 <tr>
个标签,您可以使用此列表理解:
listt2 = [s.span.text for s in soup.select('tr:has(span)') if s.span.text]
编辑:
from bs4 import BeautifulSoup
html_doc = '''<tr>
<td colspan="2" class="cell--section-end cell--link cell--link__icon">
<a data-analytics="[Competitions] - German Bundesliga" href="/football/german-bundesliga/event/26301018" class="cell--link__link cell-text">
<i class="i accordion__title-icon--green accordion__title-icon--right" data-char=""></i> <b class="cell-text__line cell-text__line--icon">
<span class="competitions-team-name js-ev-desc">1. FC Köln v 1899 Hoffenheim</span>
</b>
</a>
</td>
<tr>'''
soup = BeautifulSoup(html_doc, 'html.parser')
listt2 = [s.span.text for s in soup.select('tr:has(span)') if s.span.text]
print(listt2)
打印:
['1. FC Köln v 1899 Hoffenheim']
您只需在查找 span.text
之前检查 span is not None
。
listt2 = [s.span.text for s in soup.find_all('tr') if s.span is not None and s.span.text]
因为 short-circuiting,如果 s.span is None
则永远不会评估 s.span.text
因为 False and *
是 False