问题
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