如果两个跨度具有相同的 class,如何从 bs4.BeautifulSoup 字符串中获取文本?
How to get text from bs4.BeautifulSoup string if two span have same class?
data = "Purchase requests: <span class="market_commodity_orders_header_promot">6008067</span><br>Start price: <span class="market_commodity_orders_header_promote">15,84 pуб.</span>"
sosoup = BeautifulSoup(data, "html.parser")
ququotes = sosoup.find_all('span', class_="market_commodity_orders_header_promote")
print(ququotes)
这个代码return我
[<span class="market_commodity_orders_header_promote">6007813</span>, <span class="market_commodity_orders_header_promote">15,82 pуб.</span>]
如果我尝试
ququotes = sosoup.find('span', class_="market_commodity_orders_header_promote").get_text()
我return只有第一个号码,如何获得第二个号码?
试试这个:
ququotes = [i.get_text() for i in sosoup.find_all('span', class_="market_commodity_orders_header_promote")]
或
f = sosoup.find_all('span', class_="market_commodity_orders_header_promote")[0].get_text()
s = sosoup.find_all('span', class_="market_commodity_orders_header_promote")[1].get_text()
有很多方法,select by index:
sosoup.find_all('span', class_="market_commodity_orders_header_promote")[1].text
首先找到它的下一个 <span>
:
sosoup.find('span', class_="market_commodity_orders_header_promote").find_next('span').text
或 css selectors
和下一个兄弟 (~):
sosoup.select_one('span.market_commodity_orders_header_promote ~ span').text
data = "Purchase requests: <span class="market_commodity_orders_header_promot">6008067</span><br>Start price: <span class="market_commodity_orders_header_promote">15,84 pуб.</span>"
sosoup = BeautifulSoup(data, "html.parser")
ququotes = sosoup.find_all('span', class_="market_commodity_orders_header_promote")
print(ququotes)
这个代码return我
[<span class="market_commodity_orders_header_promote">6007813</span>, <span class="market_commodity_orders_header_promote">15,82 pуб.</span>]
如果我尝试
ququotes = sosoup.find('span', class_="market_commodity_orders_header_promote").get_text()
我return只有第一个号码,如何获得第二个号码?
试试这个:
ququotes = [i.get_text() for i in sosoup.find_all('span', class_="market_commodity_orders_header_promote")]
或
f = sosoup.find_all('span', class_="market_commodity_orders_header_promote")[0].get_text()
s = sosoup.find_all('span', class_="market_commodity_orders_header_promote")[1].get_text()
有很多方法,select by index:
sosoup.find_all('span', class_="market_commodity_orders_header_promote")[1].text
首先找到它的下一个 <span>
:
sosoup.find('span', class_="market_commodity_orders_header_promote").find_next('span').text
或 css selectors
和下一个兄弟 (~):
sosoup.select_one('span.market_commodity_orders_header_promote ~ span').text