美丽的汤查找 - 获取文本

时间:2022-11-29 13:11:14

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?

我有一些代码只是作为字符串吐出价格(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>

2 个解决方案

#1


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

您必须调用price变量的get_text()方法:

print(price.get_text())

#2


You use get_text() on your soup tag.

你在汤标签上使用get_text()。

print(price.get_text())

#1


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

您必须调用price变量的get_text()方法:

print(price.get_text())

#2


You use get_text() on your soup tag.

你在汤标签上使用get_text()。

print(price.get_text())