How do I extract text after <i class> tag?

Viewed 753

I am trying to print out the text 'Dealer' from div class by using beautifulSoup, but I do not know how to extract it.

I tried to print the i class, but the text Dealer did not come out

url = 'https://www.carlist.my/used-cars-for-sale/proton/malaysia'
response = requests.get(url, params={'page_number': 1})
soup = BeautifulSoup(response.text, 'lxml')
articles = soup.find_all('article')[:25]
seller_type = articles[4].find('div', class_ = 'item push-quarter--ends listing__spec--dealer')
seller_type_text = articles[4].find('i', class_ = 'icon icon--secondary muted valign--top push-quarter--right icon--user-formal')

print(seller_type.prettify())
print()
print(seller_type_text)

This is the output that I got:

<div class="item push-quarter--ends listing__spec--dealer">
 <i class="icon icon--secondary muted valign--top push-quarter--right icon--user-formal">
 </i>
 Dealer
 <span class="flyout listing__badge listing__badge--trusted-seller inline--block valign--top push-quarter--left">
  <i class="icon icon--thumb-up">
  </i>
  <span class="flyout__content flyout__content--tip visuallyhidden--portable">
   This 'Trusted Dealer' has a proven track record of upholding the best car selling practices certified by Carlist.my
  </span>
 </span>
 <!-- used car -->
 <!-- BMW -->
</div>


<i class="icon icon--secondary muted valign--top push-quarter--right icon--user-formal"></i>

How do I print the word 'Dealer' right after i class and before the span class?

Can someone please help me?

Thanks a lot!

3 Answers

There is a faster way of using one of the compound class names of the i tag element along with next_sibling.

If you examine the html you can see "Dealer" is part of the parent div of the i tag, and follows the i tag; so, you can grab the i tag then use next_sibling

enter image description here

from bs4 import BeautifulSoup as bs
import requests

r = requests.get('https://www.carlist.my/used-cars-for-sale/proton/malaysia')
soup = bs(r.content, 'lxml')
print(soup.select_one('.icon--user-formal').next_sibling)

Take a look at the contents property of your seller_type. You'll see that Dealer is at seller_type.contents[2]. In other words,

import requests
from bs4 import BeautifulSoup
url = 'https://www.carlist.my/used-cars-for-sale/proton/malaysia?profile_type=Dealer'
response = requests.get(url, params={'page_number': 1})
soup = BeautifulSoup(response.text, 'lxml')
articles = soup.find_all('article')[:25]
seller_type = articles[4].find('div', class_ = 'item push-quarter--ends listing__spec--dealer')
print(seller_type.contents[2])
import requests
from bs4 import BeautifulSoup
url = 'https://www.carlist.my/used-cars-for-sale/proton/malaysia?profile_type=Dealer'
response = requests.get(url, params={'page_number': 1})
soup = BeautifulSoup(response.text, 'lxml')
articles = soup.find_all('article')[:25]
seller_type = articles[4].find('div', class_ = 'item push-quarter--ends listing__spec--dealer')
print(seller_type.contents[2])
Related