BeautifulSoup/Python/HTML - Return div class 紧跟在特定 div class 之后
BeautifulSoup/Python/HTML - Return the div class right after a specific div class
示例:
<div class="label">Employee Count</div>
<div class="field">331,000</div>
如何使用 beautiful soup(或不同的 python 库)在 HTML 文件中搜索 "Employee Count" 然后 return 出现的值 (331,000)紧随其后?
使用
result = soup.body.find(text='Employee Count')
我可以找到 Employee Count,但是我怎么才能 return 它后面的字段?
找到带有 Employee Count
文本的 div
元素并得到 next sibling:
soup.find('div', text='Employee Count').find_next_sibling().text
演示:
>>> from bs4 import BeautifulSoup
>>> data = """
... <body>
... <div class="label">Employee Count</div>
... <div class="field">331,000</div>
... </body>
... """
>>>
>>> soup = BeautifulSoup(data)
>>> soup.find('div', text='Employee Count').find_next_sibling().text
331,000
示例:
<div class="label">Employee Count</div>
<div class="field">331,000</div>
如何使用 beautiful soup(或不同的 python 库)在 HTML 文件中搜索 "Employee Count" 然后 return 出现的值 (331,000)紧随其后?
使用
result = soup.body.find(text='Employee Count')
我可以找到 Employee Count,但是我怎么才能 return 它后面的字段?
找到带有 Employee Count
文本的 div
元素并得到 next sibling:
soup.find('div', text='Employee Count').find_next_sibling().text
演示:
>>> from bs4 import BeautifulSoup
>>> data = """
... <body>
... <div class="label">Employee Count</div>
... <div class="field">331,000</div>
... </body>
... """
>>>
>>> soup = BeautifulSoup(data)
>>> soup.find('div', text='Employee Count').find_next_sibling().text
331,000