1

I have a HTML file and I want to find out the <tr> tags whose id begins with "tr" like "id=tr3245", "id=tr8796" etc.

<tr id=tr1256>
  ....
</tr>
<tr id=tr5847>
  ....
</tr>
<tr id=tr8746>
  ....
</tr>
<tr id=tr9844>
  ....
</tr>

How can I do this with "beautiful soup"?

1 Answer 1

2

Use BeautifulSoup.select with tr[id^="tr"] css selector (See Beautiful Soup Documentation - CSS Selector):

from bs4 import BeautifulSoup

html = '''
<tr id=tr1256>
  ....
</tr>
<tr id=tr5847>
  ....
</tr>
<tr id=tr8746>
  ....
</tr>
<tr id=tr9844>
  ....
</tr>
'''

soup = BeautifulSoup(html)
for tr in soup.select('tr[id^="tr"]'):
    print(tr.get('id'))

prints

tr1256
tr5847
tr8746
tr9844
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.