我正在尝试使用BeautifulSoup访问字符串Out of Stock
,但找不到它的方法:
<span style="color: #727272; font-size: 14px; font-weight: normal;">
<strong>Price: $790</strong>
(Out of stock)
</span>
任何人都可以提示我该如何做到这一点?
答案 0 :(得分:1)
使用.next_sibling
attribute获取<strong>
代码后的元素:
span.strong.next_sibling
该字符串周围可能有额外的空格,因此您可以使用str.strip()
来清理它。
演示:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <span style="color: #727272; font-size: 14px; font-weight: normal;">
... <strong>Price: $790</strong>
... (Out of stock)
... </span>
... ''')
>>> soup.span.strong
<strong>Price: $790</strong>
>>> soup.span.strong.next_sibling
u'\n (Out of stock)\n'
>>> soup.span.strong.next_sibling.strip()
u'(Out of stock)'
答案 1 :(得分:1)
import bs4
soup = bs4.BeautifulSoup(html_text)
soup.get_text().split('\n')[2].strip()