【发布时间】:2015-07-17 12:27:05
【问题描述】:
我有这段代码仅将价格作为字符串 (125.01) 吐出,但我必须更改某些内容,因为现在它使用 html 标记和所有内容打印整行。我怎样才能让它只打印出文本,而不使用正则表达式?
import requests
from bs4 import BeautifulSoup
url = 'http://finance.yahoo.com/q?s=aapl&fr=uh3_finance_web&uhb=uhb2'
data = requests.get(url)
soup = BeautifulSoup(data.content)
price = soup.find("span", {'id':'yfs_l84_aapl'})
print(price)
<span id="yfs_l84_aapl">125.01</span>
【问题讨论】:
-
已解决。我添加了 .get_text() 方法并且它有效。谢谢@halex
标签: python web beautifulsoup