因此,在我正在構建的專案中,我想找到使用findAll()命令獲得的多個結果中包含的價格。這是代碼:
soup = BeautifulSoup(driver.page_source, 'html.parser')
price = soup.find_all(class_='search-result__market-price--value')
print(price)
這就是我得到的:
[<span class="search-result__market-price--value" tabindex="-1"> $0.11 </span>, <span class="search-result__market-price--value" tabindex="-1"> $0.24 </span>, ... ]
我嘗試使用我在其他地方找到的這段代碼,price = soup.find_all(class_='search-result__market-price--value')[0].string但它只是給出了錯誤IndexError: list index out of range。
我該怎么做才能得到數字?
uj5u.com熱心網友回復:
迭代ResultSet創建者find_all():
soup = BeautifulSoup(driver.page_source, 'html.parser')
for price in soup.find_all(class_='search-result__market-price--value'):
print(price.text)
或者只是得到數字
print(price.text.split('$')[-1])
例子
from bs4 import BeautifulSoup
html='''
<span tabindex="-1"> $0.11 </span>
<span tabindex="-1"> $0.24 </span>
'''
soup = BeautifulSoup(html, 'html.parser')
for tag in soup.find_all('span'):
print(tag.text.split('$')[-1])
輸出
0.11
0.24
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/492305.html
上一篇:如何將資料抓取到excel檔案中
