如何解决使用Python在html中的某些文本之后找到最接近的表格的方法
我正在使用BeautifulSoup解析HTML数据。
我正在解析的HTML看起来像
<html>
<head></head>
<body>
<table class = "nb">
<tr>
<td> <p> ABC </p> </td>
<tr>
</table>
<table border = "1">
...
</table>
<table class = "nb">
<tr>
<td> <p> DEF </p> </td>
<tr>
</table>
<table border = "1">
...
</table>
<table class = "nb">
<tr>
<td> <p> GHI </p> </td>
<tr>
</table>
<table border = "1">
...
</table>
<body>
</html>
(ABC,DEF和GHI是下表的名称)
在此html中,我需要做的是。
首先,检查html中是否有“ ABC”或“ GHI”文本。
第二,在(next_silbings)文本“ ABC”和“ GHI”之后找到第一张表(因此第一张和第三张表的边框为“ 1”)
(换句话说,我需要找到表的名称(ABC),并找到带有border =“ 1”的第一个表)
首先,使用
findAll(text = "regular expression")
我可以解决第一个问题。
实现第二秒我尝试使用next_siblings或类似的方法,但是我没有达到第二秒。
谢谢。
解决方法
另一种方法。
from simplified_scrapy import SimplifiedDoc
html = '''
<html>
<head></head>
<body>
<table class = "nb">
<tr>
<td> <p> ABC </p> </td>
<tr>
</table>
<table border = "1">
...
</table>
<table class = "nb">
<tr>
<td> <p> DEF </p> </td>
<tr>
</table>
<table border = "1">
...
</table>
<table class = "nb">
<tr>
<td> <p> GHI </p> </td>
<tr>
</table>
<table border = "1">
...
</table>
<body>
</html>
'''
doc = SimplifiedDoc(html)
# First,find and check there is "ABC" or "GHI" text in html.
nameTable = doc.getElementByReg('ABC',tag='td')
if nameTable:
nameTable = nameTable.getParent('table')
# Second,find the first table after(next_silbings) text "ABC"
table = nameTable.getNext('table') # Using next
print(table['border'])
# Or,Using index positioning
table = doc.getElement('table',start=nameTable._end)
print(table['border'])
结果:
1
1
还有更多示例:https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。