<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>
<!-- required First 3 column names-->
<tr>
<td></td>
<td align="center" style="vertical-align:middle;" title=" Number">Number</td>
<td align="center" style="vertical-align:middle;" colspan="2" title=" Code ">CODE</td>
<td align="center" style="vertical-align:middle;">Name</td>
<td align="center" style="vertical-align:middle;" title="Category ">Category</td>
<td align="center" style="vertical-align:middle;" title="Variation ">Variation</td>
</tr>
<!-- required -->
<tr class="success">
<td align="center" style="vertical-align:middle;"></td>
<td align="center" style="vertical-align:middle;"><a href="/Number/3259">3259</a>
</td>
<td style="width:125px;height:60px;vertical-align:middle;" <a href="/CODE/ABC123">ABC123</a>
</td>
<td align="center" style="vertical-align:middle;"><a title="CODE ABC123" href="/CODE/ABC123">LAND </a>
</td>
<td align="center" style="vertical-align:middle;" class="success">3 - Design Reference</td>
<td align="center" style="vertical-align:middle;" class="success">2 - Production Item</td>
</tr>
<!-- not-required -->
<tr>
<td colspan="7">
<div id="BMCRL_36" class="collapse">
<div class="list-group">
<a class="list-group-item">
<h4 class="list-group-item-heading"> Number 3259 - Reference Number ABC123</h4>
<p class="list-group-item-text"></p>
</a>
<a class="list-group-item">
<h4 class="list-group-item-heading">CODE ABC123 </h4>
<p class="list-group-item-text"></p>
</a>
</div>
</div>
</td>
</tr>
<!-- required -->
<tr class="">
<td align="center" style="vertical-align:middle;"></td>
<td align="center" style="vertical-align:middle;"><a href="/Number/3260">3260</a>
</td>
<td style="width:125px;height:60px;vertical-align:middle;" <a href="/CODE/XYZ453">XYZ453</a>
</td>
<td align="center" style="vertical-align:middle;"><a title="CODE XYZ453" href="/CODE/XYZ453">WATER </a>
</td>
<td align="center" style="vertical-align:middle;" class="success">3 Control Reference</td>
<td align="center" style="vertical-align:middle;" class="success">2 Item</td>
</tr>
</tbody>
</table>
</div>
输出应该是
- 我使用它时来自First'td'标签的列名
from bs4 import BeautifulSoup soup = BeautifulSoup(open('code.html'),'lxml') col = soup.find('tr').find_all('td') for c in col: print(c.get_text())
所有列都打印出来。相反,我只需要,['Number', 'Code', 'Name']
- 我不需要'tr'包含“colspan” (第3个tr)
- 同时删除最后两列值,
tr = soup.findChildren('tr') for t in tr: td = t.findChildren('td') for child in td: print(child.text)
再次,我得到包括列和值的整个数据,我期望的是['3259', 'ABC123', 'LAND'] and ['3260', 'XYZ453','WATER']
通过去除['3 - Design Reference','2 - Production Item']
将结果切片find_all()
以仅获取相关的列和单元格。要仅获取相关行,请检查class
属性是否存在。
工作样本:
import requests
from bs4 import BeautifulSoup
data = """
your HTML here
"""
soup = BeautifulSoup(data, "lxml")
headers = [c.get_text() for c in soup.find('tr').find_all('td')[1:4]]
data = [[cell.get_text(strip=True) for cell in row.find_all('td')[1:4]]
for row in soup.find_all("tr", class_=True)]
print(headers)
print(data)
复制
打印:
['Number', 'CODE', 'Name']
[['3259', 'ABC123', 'LAND'], ['3260', 'XYZ453', 'WATER']]
复制
赞0收藏0评论0分享
用户回答回答于 2018-10-18
继Alecxe的有用答案之后,为了更具可读性和Pythonic方法,可以利用列表推导来排除特定的BS4结果(包括那些不符合设定标准的结果)。这是一个例子:
rows = [ row for row in soup.findAll('tr') if 'adsbygoogle' not in row.text ]