美丽的汤查找 - 得到的只是文字 [英] Beautiful Soup Find - get just the text
本文介绍了美丽的汤查找 - 得到的只是文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有code吐痰该位刚刚出来的价格为字符串(125.01),但因为现在它打印的HTML标签,一切全行我一定已经改变了一些东西。我怎样才能得到它打印出来只是文本,不使用常规的前pressions?
进口要求
从BS4进口BeautifulSoupURL ='http://finance.yahoo.com/q?s=aapl&fr=uh3_finance_web&uhb=uhb2数据= requests.get(URL)
汤= BeautifulSoup(data.content)
价格= soup.find(跨越,{ID:yfs_l84_aapl'})
打印(价格)
&所述;跨度的id =yfs_l84_aapl> 125.01&下; /跨度>
解决方案
您必须调用的 get_text()
你的价格法
变量:
打印(price.get_text())
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>
解决方案
You have to call the get_text()
method of your price
variable:
print(price.get_text())
这篇关于美丽的汤查找 - 得到的只是文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文