【发布时间】:2017-01-16 16:28:30
【问题描述】:
我需要通过部分文本获取 HTML 标记。我找到了一些解决方案,但对我来说效果不佳。
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup("""
<table>
<tbody>
<tr>
<td style="width: 100px; height: 20px">
<div style="font-size: 8.7pt">
Арт.:
<span id="ContentPlaceHolder1_ContentPlaceHolder1_DataList2_Label12_0"> 1185A</span>
</div>
<div style="font-size: 12pt; font-weight: bold;">
<span id="ContentPlaceHolder1_ContentPlaceHolder1_DataList2_LoginView3_0_Label12_0">I_CAN_GET_THIS other text</span>
I CAN NOT GET THIS?.
</div>
</td>
</tr>
</tbody>
</table>
""", 'lxml')
print(soup.find('span', text=re.compile('I_CAN_GET_THIS')))
print(soup.find('div', text=re.compile('I_CAN_NOT_GET_THIS')))
>>> <span id="ContentPlaceHolder1_ContentPlaceHolder1_DataList2_LoginView3_0_Label12_0">I_CAN_GET_THIS other text</span>
>>> None
所以我不明白为什么它在第二种情况下不起作用,我应该怎么做才能使它起作用? 谢谢
【问题讨论】:
标签: regex python-3.x beautifulsoup