'How to parse this code to get a text from 'span' (Beautiful Soup)
help me please , i cant get a string("Kyiv") from here ( using beautiful soup. all i can get is 'None'
HTML TO PARSE:
<div class="space rel">
<p class="lheight16">
<small class="breadcrumb x-normal">
<span><i data-icon="location-filled"></i>Kyiv</span>
</small>
Solution 1:[1]
Try:
from bs4 import BeautifulSoup
html='''
<div class="space rel">
<p class="lheight16">
<small class="breadcrumb x-normal">
<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
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | F.Hoque |
| Solution 2 | Andrej Kesely |
