請幫幫我,我不能從這里得到一個字串(“Kyiv”)(使用漂亮的湯。我能得到的只是“無”
要決議的 HTML:
<div class="space rel">
<p class="lheight16">
<small class="breadcrumb x-normal">
<span><i data-icon="location-filled"></i>Kyiv</span>
</small>
uj5u.com熱心網友回復:
嘗試:
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)
輸出:
Kyiv
uj5u.com熱心網友回復:
此示例將獲取有關廣告的資訊(標題、價格和位置):
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)
印刷:
Дубленка Stradivarius 5738/401/147 M Бледно-розовая (05738401147035)
650 грн.
Бровары
--------------------------------------------------------------------------------
Крос?вки чолов?ч? Nike 270 React кроссовки мужские найк 270 реакт
1 619 грн.
Львов, Шевченковский
--------------------------------------------------------------------------------
Крос?вки adidas ориг?нал
1 500 грн.
Каменец-Подольский
--------------------------------------------------------------------------------
Garneau 39 розм?р Велотуфл? / шипи
750 грн.
Червоноград
--------------------------------------------------------------------------------
...and so on.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461353.html
