使用 urllib.request 获取股票数据

发布于 2024-12-21 23:40:28 字数 2451 浏览 2 评论 0原文

我编写了以下代码来检索有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后来的我们 2024-12-28 23:40:28

如果您的所有请求都发送至同一个域,我建议使用 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.

折戟 2024-12-28 23:40:28

您可以通过一次调用 request.urlopen 来请求多只股票的信息

import urllib.request as request
import urllib.parse as parse
import csv
import codecs
import pprint

def printdata(stks):
    params = parse.urlencode((('s', '+'.join(stks)), ('f', 'sl1rvwb4j4r5')))
    url = 'http://finance.yahoo.com/d/quotes.csv'
    url = '?'.join((url, params))
    req = request.urlopen(url)
    f = codecs.getreader('utf8')(req)
    fields = '''Ticker Price PE_Ratio Volume Year_Range Book_Value_per_Share
              EBITDA PEG_Ratio'''.split()
    for row in csv.reader(f):
        stkdata = dict(zip(fields, row))        
        pprint.pprint(stkdata)

printdata('YHOO GOOG MSFT'.split())

{'Book_Value_per_Share': '10.051',
 'EBITDA': '1.406B',
 'PEG_Ratio': '1.47',
 'PE_Ratio': '18.56',
 'Price': '14.96',
 'Ticker': 'YHOO',
 'Volume': '32625192',
 'Year_Range': '11.09 - 18.84'}
{'Book_Value_per_Share': '169.355',
 'EBITDA': '13.446B',
 'PEG_Ratio': '0.89',
 'PE_Ratio': '21.12',
 'Price': '625.96',
 'Ticker': 'GOOG',
 'Volume': '4459782',
 'Year_Range': '473.02 - 642.96'}
{'Book_Value_per_Share': '7.062',
 'EBITDA': '30.146B',
 'PEG_Ratio': '0.98',
 'PE_Ratio': '9.29',
 'Price': '26.00',
 'Ticker': 'MSFT',
 'Volume': '101410080',
 'Year_Range': '23.65 - 29.46'}

You can request info for multiple stocks with one call to request.urlopen:

import urllib.request as request
import urllib.parse as parse
import csv
import codecs
import pprint

def printdata(stks):
    params = parse.urlencode((('s', '+'.join(stks)), ('f', 'sl1rvwb4j4r5')))
    url = 'http://finance.yahoo.com/d/quotes.csv'
    url = '?'.join((url, params))
    req = request.urlopen(url)
    f = codecs.getreader('utf8')(req)
    fields = '''Ticker Price PE_Ratio Volume Year_Range Book_Value_per_Share
              EBITDA PEG_Ratio'''.split()
    for row in csv.reader(f):
        stkdata = dict(zip(fields, row))        
        pprint.pprint(stkdata)

printdata('YHOO GOOG MSFT'.split())

yields

{'Book_Value_per_Share': '10.051',
 'EBITDA': '1.406B',
 'PEG_Ratio': '1.47',
 'PE_Ratio': '18.56',
 'Price': '14.96',
 'Ticker': 'YHOO',
 'Volume': '32625192',
 'Year_Range': '11.09 - 18.84'}
{'Book_Value_per_Share': '169.355',
 'EBITDA': '13.446B',
 'PEG_Ratio': '0.89',
 'PE_Ratio': '21.12',
 'Price': '625.96',
 'Ticker': 'GOOG',
 'Volume': '4459782',
 'Year_Range': '473.02 - 642.96'}
{'Book_Value_per_Share': '7.062',
 'EBITDA': '30.146B',
 'PEG_Ratio': '0.98',
 'PE_Ratio': '9.29',
 'Price': '26.00',
 'Ticker': 'MSFT',
 'Volume': '101410080',
 'Year_Range': '23.65 - 29.46'}
蓝海似她心 2024-12-28 23:40:28

您可以使用threadingmultiprocessing模块同时获取所有这些URL,这样您就可以节省大量时间,因为获取都是单独的,与其他人无关。

You can use threading or multiprocessing 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文