使用 urllib.request 获取股票数据
我编写了以下代码来检索有关 S&P 500 股票的数据。该代码可以工作,但由于 urlopen 请求的数量,速度非常慢。我可以使用什么策略来加快速度?
from urllib.request import urlopen
import csv
class StockQuote:
"""gets stock data from Yahoo Finance"""
def __init__(self, quote):
self.quote = quote
def lastPrice(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=l1'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def volume(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=v0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def yearrange(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=w0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEratio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def bookValue(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=b4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def EBITDA(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=j4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEGRatio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r5'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def ticker(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=s0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def openSP500file():
SP500 = csv.reader(open(r'C:\Users\dev\Desktop\SP500.csv', 'r'), delimiter=',')
for x in SP500:
indStk = x[0]
printdata(indStk)
def printdata(stk):
stkObj = StockQuote(stk)
stkdata= {}
stkdata['Ticker'] = stkObj.ticker()
stkdata['Price'] = stkObj.lastPrice()
stkdata['PE Ratio'] = stkObj.PEratio()
stkdata['Volume'] = stkObj.volume()
stkdata['Year Range'] = stkObj.yearrange()
stkdata['Book Value per Share'] = stkObj.bookValue()
stkdata['EBITDA'] = stkObj.EBITDA()
stkdata['PEG Ratio'] = stkObj.PEGRatio()
print(stkdata)
def main():
openSP500file()
if __name__ == '__main__':
main()
谢谢!
I wrote the following code to retrieve data about stocks in the S&P 500. The code works, but it is very slow due to the number of urlopen requests. What strategies can I use to speed this up?
from urllib.request import urlopen
import csv
class StockQuote:
"""gets stock data from Yahoo Finance"""
def __init__(self, quote):
self.quote = quote
def lastPrice(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=l1'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def volume(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=v0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def yearrange(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=w0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEratio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def bookValue(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=b4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def EBITDA(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=j4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEGRatio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r5'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def ticker(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=s0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def openSP500file():
SP500 = csv.reader(open(r'C:\Users\dev\Desktop\SP500.csv', 'r'), delimiter=',')
for x in SP500:
indStk = x[0]
printdata(indStk)
def printdata(stk):
stkObj = StockQuote(stk)
stkdata= {}
stkdata['Ticker'] = stkObj.ticker()
stkdata['Price'] = stkObj.lastPrice()
stkdata['PE Ratio'] = stkObj.PEratio()
stkdata['Volume'] = stkObj.volume()
stkdata['Year Range'] = stkObj.yearrange()
stkdata['Book Value per Share'] = stkObj.bookValue()
stkdata['EBITDA'] = stkObj.EBITDA()
stkdata['PEG Ratio'] = stkObj.PEGRatio()
print(stkdata)
def main():
openSP500file()
if __name__ == '__main__':
main()
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的所有请求都发送至同一个域,我建议使用 urllib3。它不在标准 python 安装中,但它实现了连接池,因此所有单独的请求都更快。
If all of your requests are going to the same domain, I would suggest using urllib3. It's not in the standard python install, but it implements connection pooling so all the individual requests are faster.
您可以通过一次调用
request.urlopen
来请求多只股票的信息:
You can request info for multiple stocks with one call to
request.urlopen
:yields
您可以使用
threading
或multiprocessing
模块同时获取所有这些URL,这样您就可以节省大量时间,因为获取都是单独的,与其他人无关。You can use
threading
ormultiprocessing
module to fetch all those URLs in same time, so you can save a lot of time since the fetching are all individual not related to others.