python - 使用 python3-beautifulsoup3 从 HTML 中抓取字符串
问题描述
我正在尝试使用beautifulsoup 从表格行中获取字符串。我想得到的字符串是'SANDAL'和'SHORTS',来自第二行和第三行。我知道这可以用正则表达式或字符串函数来解决,但我想学习beautifulsoup 并尽可能多地使用beautifulsoup。
剪辑的python代码
soup=beautifulsoup(page,'html.parser')
table=soup.find('table')
row=table.find_next('tr')
row=row.find_next('tr')
HTML
<html>
<body>
<div id="body">
<div class="data">
<table id="products">
<tr><td>PRODUCT<td class="ole1">ID<td class="c1">TYPE<td class="ole1">WHEN<td class="ole4">ID<td class="ole4">ID</td></tr>
<tr><td>SANDAL<td class="ole1">77313<td class="ole1">wear<td class="ole1">new<td class="ole4">id<td class="ole4">878717</td></tr>
<tr><td>SHORTS<td class="ole1">77314<td class="ole1">wear<td class="ole1">new<td class="ole4">id<td class="ole4">878718</td></tr>
</table>
</div>
</div>
</body>
</html>
解决方案
要从表格的第一列(无标题)获取文本,您可以使用以下脚本:
from bs4 import BeautifulSoup
txt = '''
<html>
<body>
<div id="body">
<div class="data">
<table id="products">
<tr><td>PRODUCT<td class="ole1">ID<td class="c1">TYPE<td class="ole1">WHEN<td class="ole4">ID<td class="ole4">ID</td></tr>
<tr><td>SANDAL<td class="ole1">77313<td class="ole1">wear<td class="ole1">new<td class="ole4">id<td class="ole4">878717</td></tr>
<tr><td>SHORTS<td class="ole1">77314<td class="ole1">wear<td class="ole1">new<td class="ole4">id<td class="ole4">878718</td></tr>
</table>
</div>
</div>
</body>
</html>'''
soup = BeautifulSoup(txt, 'lxml') # <-- lxml is important here (to parse the HTML code correctly)
for tr in soup.find('table', id='products').find_all('tr')[1:]: # <-- [1:] because we want to skip the header
print(tr.td.text) # <-- print contents of first <td> tag
印刷:
SANDAL
SHORTS
推荐阅读
- arduino - 为什么 Adafruit 而不是 fastLED 与 WS2812 LED 灯条和 Arduino Nano 一起使用?
- hyperledger-fabric - 每个备份/节点如何在 PBFT 中获得 2f 个回复?
- scala - 加特林模拟未终止
- java - Android:RESTful JDBC 代理。性能和安全
- r - 如何根据另一个变量的一系列值创建一个标识唯一组的变量?
- javascript - 根据表单输入Javascript获取数据
- javascript - 使用 HTML 元素显示关联数组值
- javascript - 如何使以下功能仅在右键单击时起作用?
- pandas - Pandas 的 pivot_table 正在删除 NaT 日期
- docker - Pod 没有正确响应