Beautiful Soup Find - get just the text

Deadly 提交于 2019-12-20 04:38:37

问题


I had this bit of code spitting out just the price as a string (125.01), but I must have changed something because now it prints the whole line with the html tags and everything. How can i get it to print out just the text, without using regular expressions?

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>

回答1:


You have to call the get_text() method of your price variable:

print(price.get_text())



回答2:


You use get_text() on your soup tag.

print(price.get_text())


来源:https://stackoverflow.com/questions/30092474/beautiful-soup-find-get-just-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!