Home > Net >  How to extract the number after strong?
How to extract the number after strong?

Time:04-07

please tell me how to extract the value after strong (6666666666)

<div >
<p><strong>Text</strong> TextTextText</p>
<p><strong>Text2</strong> 6666666666</p>
<p><strong>Text3</strong> 1111111111</p>
</div>
I try like this:
print(soup.select('.general-section :nth-child(2)'))
print(soup.select('.general-section :nth-child(2) > strong.next_sibling'))

Sorry, I just started learning parsing

CodePudding user response:

You are close to your goal with the css selector but based on your example you have to use next_sibling based on your element:

html = '''
<div >
<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()

Output:

6666666666
  • Related