Python Forum
Using BeautifulSoup And Getting -1 Results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using BeautifulSoup And Getting -1 Results
#11
Can also do in Bs4 without convert to string(str).
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
Axel_Erfurt likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use BeautifulSoup to parse google search results DevinGP 16 21,393 Dec-22-2017, 10:23 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020