Python Forum
Problem with searching over Beautiful Soap object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with searching over Beautiful Soap object
#4
from bs4 import BeautifulSoup

html = '''\
<li>
  <span class="a-list-item">
    <span class="a-text-bold">
      Publisher
    </span>
    <span>
      Springer; 1st ed. 2020 edition (April 27, 2020)
    </span>
  </span>
</li>

<li>
  <span class="a-list-item">
    <span class="a-text-bold">
      Publication date
    </span>
    <span>
      April 27, 2020
    </span>
  </span>'''

soup = BeautifulSoup(html, 'lxml')
>>> tag = soup.find('span', class_="a-list-item")
>>> tag.find_all('span')[0].text.strip()
'Publisher'
>>> tag.find_all('span')[1].text.strip()
'Springer; 1st ed. 2020 edition (April 27, 2020)
Also remember that BS support CSS Selectors .
>>> tag = soup.select_one('span > span:nth-child(2)')
>>> tag.text.strip()
'Springer; 1st ed. 2020 edition (April 27, 2020)'
This is what i use most,as when look in browser(inspect) can copy Selector then get the path as show here automatically.
Reply


Messages In This Thread
RE: Problem with searching over Beautiful Soap object - by snippsat - May-15-2022, 02:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beautifull Soap. Split page using a value and not a tag. lillo123 5 3,502 Apr-21-2021, 09:11 AM
Last Post: lillo123
  Beautiful Soap can't find a specific section on the page Pavel_47 1 2,477 Jan-18-2021, 02:18 PM
Last Post: snippsat
  Beautiful soup and tags starter_student 11 6,363 Jul-08-2019, 03:41 PM
Last Post: starter_student
  Beautiful Soup find_all() kirito85 2 3,432 Jun-14-2019, 02:17 AM
Last Post: kirito85
  form.populate_obj problem "object has no attribute translate" pascale 0 3,696 Jun-12-2019, 07:30 PM
Last Post: pascale
  Need help with Beautiful Soup - table jlkmb 9 6,046 Dec-20-2018, 01:10 AM
Last Post: jlkmb
  Type Not Found error on python soap call using suds library wellborn 1 4,657 Dec-19-2017, 07:53 PM
Last Post: micseydel
  Help with beautiful soup Larz60+ 5 4,528 Jul-18-2017, 08:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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