strong之后如何提取数字?
How to extract the number after strong?
请告诉我如何提取strong(6666666666)后的值
<div class="general-section">
<p><strong>Text</strong> TextTextText</p>
<p><strong>Text2</strong> 6666666666</p>
<p><strong>Text3</strong> 1111111111</p>
</div>
我这样尝试:
print(soup.select('.general-section :nth-child(2)'))
print(soup.select('.general-section :nth-child(2) > strong.next_sibling'))
抱歉,我刚开始学解析
您已接近 css selector
的目标,但根据您的示例,您必须根据您的元素使用 next_sibling
:
html = '''
<div class="general-section">
<p><strong>Text</strong> TextTextText</p>
<p><strong>Text2</strong> 6666666666</p>
<p><strong>Text3</strong> 1111111111</p>
</div>
'''
soup = BeautifulSoup(html)
soup.select_one('.general-section p:nth-child(2) strong').next_sibling.strip()
输出:
6666666666
请告诉我如何提取strong(6666666666)后的值
<div class="general-section">
<p><strong>Text</strong> TextTextText</p>
<p><strong>Text2</strong> 6666666666</p>
<p><strong>Text3</strong> 1111111111</p>
</div>
我这样尝试:
print(soup.select('.general-section :nth-child(2)'))
print(soup.select('.general-section :nth-child(2) > strong.next_sibling'))
抱歉,我刚开始学解析
您已接近 css selector
的目标,但根据您的示例,您必须根据您的元素使用 next_sibling
:
html = '''
<div class="general-section">
<p><strong>Text</strong> TextTextText</p>
<p><strong>Text2</strong> 6666666666</p>
<p><strong>Text3</strong> 1111111111</p>
</div>
'''
soup = BeautifulSoup(html)
soup.select_one('.general-section p:nth-child(2) strong').next_sibling.strip()
输出:
6666666666