[英]Beautiful Soup: Parsing “Span” element
I keep running into walls, but feel like I'm close here. 我一直跑到墙上,但觉得我就在附近。
HTML block being harvested: 正在收获的HTML块:
<div class="your-price">
<span class="label">Your Price</span>
<span class="currency">$369.99</span>
<input type="hidden" name="price" value="$369.99" />
</div>
I would like to parse out the "$369.99" value alone (currency class). 我想单独解析“369.99美元”的价值(货币类)。 Here is my logic so far, which captures both 'label' and 'currency' content: 到目前为止,这是我的逻辑,它捕获了“标签”和“货币”内容:
r = requests.get(Base_URL)
soup = BeautifulSoup(r.content)
product_price = soup.find("div", {"class": "your-price"})
print product_price.text
Thanks for your help! 谢谢你的帮助!
You can either go down by the tree and search for the span
with class="currency"
: 您可以在树下搜索并使用class="currency"
搜索span
:
print soup.find("div", class_="your-price").find("span", class_="currency").text
Or, use CSS selectors
(at least, shorter and more readable): 或者,使用CSS selectors
(至少更短,更可读):
print soup.select('div.your-price span.currency')[0].text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.