Home > Mobile >  How to parse this code to get a text from 'span' (Beautiful Soup)
How to parse this code to get a text from 'span' (Beautiful Soup)

Time:04-17

help me please , i cant get a string("Kyiv") from here ( using beautiful soup. all i can get is 'None'

HTML TO PARSE:

 <div >
                    <p >
                        <small >
                            <span><i data-icon="location-filled"></i>Kyiv</span>
                        </small>

CodePudding user response:

Try:

from bs4 import BeautifulSoup
html='''
<div >
 <p >
  <small >
   <span>
    <i data-icon="location-filled">
    </i>
    Kyiv
   </span>
  </small>
 </p>
</div>

'''
soup =BeautifulSoup(html,'html.parser')
#print(soup.prettify())

txt=soup.select_one('.breadcrumb span').get_text(strip=True)
print(txt)

Output:

Kyiv

CodePudding user response:

This example will get info about the ad (title, price and location):

import requests
from bs4 import BeautifulSoup

url = "https://www.olx.ua/moda-i-stil/odezhda/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for td in soup.select("td.offer"):
    title = td.h3.get_text(strip=True)
    price = td.select_one(".price").get_text(strip=True)
    location = td.select_one('span:has([data-icon="location-filled"])').text
    print(title)
    print(price)
    print(location)
    print("-" * 80)

Prints:

Дубленка Stradivarius 5738/401/147 M Бледно-розовая (05738401147035)
650 грн.
Бровары
--------------------------------------------------------------------------------
Кросівки чоловічі Nike 270 React кроссовки мужские найк 270 реакт
1 619 грн.
Львов, Шевченковский
--------------------------------------------------------------------------------
Кросівки adidas оригінал
1 500 грн.
Каменец-Подольский
--------------------------------------------------------------------------------
Garneau 39 розмір Велотуфлі / шипи
750 грн.
Червоноград
--------------------------------------------------------------------------------

...and so on.
  • Related