【发布时间】:2020-04-09 18:19:59
【问题描述】:
我正在创建一个用于评估股票的网络爬虫。我遇到的问题是我的代码返回一个对象“放置”(不确定应该调用什么)而不是值。
import requests
class Guru():
MedianPE = 0.0
def __init__(self, ticket):
self.ticket = ticket
try:
url = ("https://www.gurufocus.com/term/pettm/"+ticket+"/PE-Ratio-TTM/")
response = requests.get(url)
htmlText = response.text
firstSplit = htmlText
secondSplit = firstSplit.split("And the <strong>median</strong> was <strong>")[1]
thirdSplit = secondSplit.split("</strong>")[0]
lastSplit = float(thirdSplit)
try:
Guru.MedianPE = lastSplit
except:
print(ticket + ": Median PE N/A")
except:
print(ticket + ": Median PE N/A")
def getMedianPE(self):
return float(Guru.getMedianPE)
g1 = Guru("AAPL")
g1.getMedianPE
print("Median + " + str(g1))
如果我在__init__ 中打印lastSplit,它会返回我想要的值15.53,但是当我尝试通过函数getMedianPE 获取它时,我只会得到Median + <__main__.Guru object at 0x0000016B0760D288>
非常感谢您的宝贵时间!
【问题讨论】:
标签: python web-scraping stock