Home > Enterprise >  How to get text from bs4.BeautifulSoup string if two span have same class?
How to get text from bs4.BeautifulSoup string if two span have same class?

Time:03-30

data = "Purchase requests: <span >6008067</span><br>Start               price: <span >15,84 pуб.</span>"

sosoup = BeautifulSoup(data, "html.parser")
ququotes = sosoup.find_all('span', class_="market_commodity_orders_header_promote")
print(ququotes)

This code return me

[<span >6007813</span>, <span >15,82 pуб.</span>]

if i try

ququotes = sosoup.find('span', class_="market_commodity_orders_header_promote").get_text()

it's return me only first one number, how to get a second one?

CodePudding user response:

try this:

ququotes = [i.get_text() for i in sosoup.find_all('span', class_="market_commodity_orders_header_promote")]

or

f = sosoup.find_all('span', class_="market_commodity_orders_header_promote")[0].get_text()
s = sosoup.find_all('span', class_="market_commodity_orders_header_promote")[1].get_text()

CodePudding user response:

There are a lot of approaches, select by index:

sosoup.find_all('span', class_="market_commodity_orders_header_promote")[1].text

Find first and than its next <span>:

sosoup.find('span', class_="market_commodity_orders_header_promote").find_next('span').text

Or with css selectors and next sibling (~):

sosoup.select_one('span.market_commodity_orders_header_promote ~ span').text
  • Related