【问题标题】:Retrieve the text value from element in BeautifulSoup从 BeautifulSoup 中的元素中检索文本值
【发布时间】:2016-09-01 11:21:19
【问题描述】:

我正在尝试从代码中提到的 URl 中检索价格。

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.forever21.com/IN/Product/Product.aspx?
 BR=LOVE21&Category=whatsnew_app&ProductID=2000054242&VariantID=")
soup = BeautifulSoup(r.content, 'html.parser')
#print(soup.prettify())
price = soup.find_all("font",{"class":"items_price"})
print(price)

#Output:
<font class="items_price">Rs.1,249</font>

我只需要 1,249 卢比的价格 有人可以说需要做什么吗? 谢谢

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

ResultSet 是一个列表。

print (price[0].get_text())

【讨论】:

    【解决方案2】:

    简单用户.get_text()

    >>> price = soup.find("font",{"class":"items_price"})
    >>> print(price.get_text())
    'Rs.1,249'
    

    【讨论】:

    • 试过了,报错:AttributeError: 'ResultSet' object has no attribute 'get_text'
    • @KeshavReddy - 因为你使用了find_all,所以它返回多个元素,因此它返回一个ResultSet。你可以使用price[0].get_text() 或者你可以简单地使用find 来获取单个元素。
    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多