Extract element from incomplete html tag

Viewed 93

I have a piece of HTML where I don't know how I can extract the element I am looking for:

from bs4 import BeautifulSoup

s = (
    '<span class="price"><span class="mobile">Preis</span>\n'
    + '<span class="currency">CHF</span><span class="amount">110\'000</span></span>\n'
    + '<span class="region"><span class="mobile">Region</span>Ganze Schweiz</span>\n'
    + '<span class="branch"><span class="mobile">Branche</span>Gewerbe, Industrie</span>\n'
)

print(s)

soup = BeautifulSoup(s)
c = soup.find("span", class_="region")

Please note that the HTML code is incomplete, there is a <span> missing, that's a mistake on the page I am scraping.

The element I want to extract is "Ganze Schweiz" (and the corresponding other fields, "100'000" and "Gewerbe, Industrie").

Using c.find_all('span') will only get me "Region", but not the second part. I don't want to just replace "Region" with an empty string and use .text, because I have multiple lines with different words in place of "Region", so something more generic would be helpful.

Thanks for the help!

2 Answers

Try this one for generic approach.

from bs4 import BeautifulSoup

soup = BeautifulSoup('<spans class="region"><spans class="mobile">Region</spans>Ganze Schweiz</spans>', "html.parser")

value =  soup.find_all("spans")

text = value[0].getText().replace(value[1].getText(), "")

print (text)

I guess you want to find all the spans after each span which has the class="region"

You can try this:

region_spans = soup.find_all('span', class_="region")

for region_span in region_spans:
   mobile_span = region_span.find('span', class_="mobile") #look for mobile span inside each region_span
   text = mobile_span.text
   print(text)
Related