user12838973
user12838973

Reputation:

Python How i can extract data with same class name in BeautifulSoup

I'm trying to pull out data using BeautifulSoup library in python. I used zip and soup to extract.

My html data looks like this :

<li>

    <ul class="features">

        <li>Year: <strong>2016</strong></li>

        <li>Kilometers: <strong>81,000</strong></li>

    </ul>
    <ul class="features">

        <li>Doors: <strong>2 door</strong></li>

        <li>Color: <strong>White</strong></li>

    </ul>
    <ul class="features">

    </ul>

</li>

Here i want to get year,kilometers,doors,color in seperate variables. But when i run my code it getting together.

My code :


for title, price, date, features  in zip(soup.select('.listing-item .title'),
                            soup.select('.listing-item .price'),
                            soup.select('.listing-item .date'),
                            soup.select('.listing-item .features')):


    title = title.get_text().strip()
    price = price.get_text().strip()
    date = date.get_text().strip()
    features = features.get_text().strip()

    print(features)


Output :

Year: 2016
Kilometers: 81,000
Doors: 2 door
Color: White

How i can store the year,kilometers,doors,colors in seperate variables ?

Upvotes: 1

Views: 865

Answers (2)

abhilb
abhilb

Reputation: 5757

You can try:

from bs4 import BeautifulSoup as bs
from io import StringIO

data = """<li>
    <ul class="features">
        <li>Year: <strong>2016</strong></li>
        <li>Kilometers: <strong>81,000</strong></li>
    </ul>
    <ul class="features">
        <li>Doors: <strong>2 door</strong></li>
        <li>Color: <strong>White</strong></li>
    </ul>
    <ul class="features">
    </ul>
</li>"""

soup = bs(StringIO(data))
Year, Km, Doors, Color = list(map(lambda x: x.text.split(':')[1].strip(), soup.select('.features > li')))
print(Year, Km, Doors, Color)

Upvotes: 1

KunduK
KunduK

Reputation: 33384

find element li which contains text and then find next strong tag. Declare empty list and append.

Code.

from bs4 import BeautifulSoup

html='''<li>

    <ul class="features">

        <li>Year: <strong>2016</strong></li>

        <li>Kilometers: <strong>81,000</strong></li>

    </ul>
    <ul class="features">

        <li>Doors: <strong>2 door</strong></li>

        <li>Color: <strong>White</strong></li>

    </ul>
    <ul class="features">

    </ul>

</li>
'''
soup=BeautifulSoup(html,'html.parser')
Year=[]
KiloMeter=[]
Doors=[]
Color=[]
for year,km,dor,colr in zip(soup.select('ul.features li:contains("Year:")'),soup.select('ul.features li:contains("Kilometers:")'),soup.select('ul.features li:contains("Doors:")'),soup.select('ul.features li:contains("Color:")')):
    Year.append(year.find_next('strong').text)
    KiloMeter.append(km.find_next('strong').text)
    Doors.append(dor.find_next('strong').text)
    Color.append(colr.find_next('strong').text)

print(Year,KiloMeter,Doors,Color)

Output: list

['2016'] ['81,000'] ['2 door'] ['White']

Upvotes: 0

Related Questions