Python Forum
Getting a specific text inside an html with soup
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a specific text inside an html with soup
#3
How it works.
from bs4 import BeautifulSoup

html = '''\
<span><mark class="search-item__highlight">Google</mark> has warned the Trump administration</span>'''

soup = BeautifulSoup(html, 'lxml')
Use:
>>> span_tag = soup.find('span')
>>> span_tag
<span><mark class="search-item__highlight">Google</mark> has warned the Trump administration</span>

>>> span_tag.text
'Google has warned the Trump administration'
Span tag .text has has output as show over,only that Google is highlighted when html is rendered.
To find mark tag.
>>> mark_tag = span_tag.find('mark')
>>> mark_tag
<mark class="search-item__highlight">Google</mark>

>>> mark_tag.text
'Google'

# The CSS class name can be found with attrs
>>> mark_tag.attrs
{'class': ['search-item__highlight']}
Reply


Messages In This Thread
RE: Getting a specific text inside an html with soup - by snippsat - Jul-08-2019, 05:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Soup('A') new_coder_231013 6 2,813 Aug-12-2023, 10:55 AM
Last Post: Pubfonts
  Python Obstacles | Karate | HTML/Scrape Specific Tag and Store it in MariaDB BrandonKastning 8 3,327 Nov-22-2021, 01:38 AM
Last Post: BrandonKastning
  How to get specific TD text via Selenium? euras 3 9,077 May-14-2021, 05:12 PM
Last Post: snippsat
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,817 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Any way to remove HTML tags from scraped data? (I want text only) SeBz2020uk 1 3,565 Nov-02-2020, 08:12 PM
Last Post: Larz60+
  Help: Beautiful Soup - Parsing HTML table ironfelix717 2 2,797 Oct-01-2020, 02:19 PM
Last Post: snippsat
  Beautiful Soup (suddenly) doesn't get full webpage html j.crater 8 17,619 Jul-11-2020, 04:31 PM
Last Post: j.crater
  Requests-HTML vs Beautiful Soup - How to Choose? robin73 0 3,890 Jun-23-2020, 02:53 PM
Last Post: robin73
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,443 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  How to get the href value of a specific word in the html code julio2000 2 3,309 Mar-05-2020, 07:50 PM
Last Post: julio2000

Forum Jump:

User Panel Messages

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