Mar-07-2023, 02:42 PM
Can also do in Bs4 without convert to string(str).
Every tag has a Name,accessible as
Every tag has a Name,accessible as
.name
.for headings in soup.find_all(['h2', 'h3']): if headings.name == 'h2': print(f"H2, {headings.text.strip()}") else: print(f"H3, {headings.text.strip()}")
Output:H2, Best Man Speech Template
H2, Best Man Speech Tips
H2, A Best Man Speech Example to Make Your Own
H2, Best Man Speech Openers
H3, Introduce Yourself With a Twist
H3, Crack a Joke, Even a Corny One
H3, Be Hilarious With a Straight Face
H3, Introduce a Recurring Theme
H3, Ask a Question to Answer Throughout
H3, Rhyme-Master Flex
H3, Read a Definition From a Dictionary
H3, Tell a Story of How You Met
H3, Begin With a Quote
H3, Read Something in a Different Language
H3, A Guide to Wedding Reception Toasts
H2, Related Stories