簡體   English   中英

Yahoo Finance-不完整的檢索組件

[英]Yahoo Finance - incomplete retrieval components

我試圖在python中寫下一個函數來檢索Index的組件列表。 因此,可以說我想看看FTSE100(^ FTSE),我想了解它的所有組件(其中有100多個),甚至更多的信息。

我只需添加標志就可以獲取有關組件的更多信息(請參閱參考資料 )。

但是,給定索引,我只能檢索前51個組件(其第一頁: http : //finance.yahoo.com/q/cp?s=%5EFTSE&c=0 )。

我的職能是:

at = '%40'
def getListComponents(symbol):
    url = 'http://finance.yahoo.com/d/quotes.csv?s=%s%s&c=1&f=s' % (at, symbol)
    return urllib.urlopen(url).read().strip().strip('"')

Output example: 
'AAL.L"\r\n"ABF.L"\r\n"ADM.L"\r\n"ADN.L"\r\n"AGK.L"\r\n"AMEC.L"\r\n"ANTO.L"\r\n"ARM.L"\r\n"AV.L"\r\n"AZN.L"\r\n"BA.L"\r\n"BAB.L"\r\n"BARC.L"\r\n"BATS.L"\r\n"BG.L"\r\n"BLND.L"\r\n"BLT.L"\r\n"BNZL.L"\r\n"BP.L"\r\n"BRBY.L"\r\n"BSY.L"\r\n"BT-A.L"\r\n"CCL.L"\r\n"CNA.L"\r\n"CPG.L"\r\n"CPI.L"\r\n"CRDA.L"\r\n"CRH.L"\r\n"CSCG.L"\r\n"DGE.L"\r\n"ENRC.L"\r\n"EVR.L"\r\n"EXPN.L"\r\n"FRES.L"\r\n"GFS.L"\r\n"GKN.L"\r\n"GLEN.L"\r\n"GSK.L"\r\n"HL.L"\r\n"HMSO.L"\r\n"HSBA.L"\r\n"IAG.L"\r\n"IHG.L"\r\n"IMI.L"\r\n"IMT.L"\r\n"ITRK.L"\r\n"ITV.L"\r\n"JMAT.L"\r\n"KAZ.L"\r\n"KGF.L"\r\n"LAND.L'

這種獲取組件標題的方法非常簡單。

如何獲得剩余的49個組件? 考慮到,如果我使用的是FTSE250或更高版本,則未檢索到的組件可能會更多。

沒有答案:

因此,我進行了一些研究,嘗試了多種標志組合,發現並閱讀了以下注釋線程:code.google.com/p/yahoo-finance-managed/wiki/csvQuotesDownload; 並且我得出結論,不可能將索引的所有組件下載為CSV。

如果您有/曾經有過同樣的問題,則不只是使用BeautifulSoup。 您可能不喜歡這種方法,但是沒有其他方法。

解決我大部分問題

如果您這樣做的話,表格頂部會顯示last一個小鏈接-這將為您提供最后的頁碼-http: http://finance.yahoo.com/q/cp?s=%5EFTSE&c=2 s http://finance.yahoo.com/q/cp?s=%5EFTSE&c=2 (來自您的示例)然后將其拆分以創建一個范圍range(number)以循環並請求與您當前操作類似的頁面。

  1. 打開初始頁
  2. 通過lxml.html或BeautifulSoup提取鏈接
  3. 解析出最后一個頁碼
  4. 循環檢索每個頁面的頁面數

附帶一提,我很確定Yahoo! 必須為此有一個API?

我是Python的新手,正在尋找自己的腳。

我一直在尋找解決同一問題的方法,但最終還是自己寫了。 我的代碼效率低下,冗長且丑陋-但是它可以正常工作,如果很少的話我會使用。 我期待向聰明的人學習。

def getIndexComponents(symbol):

# function to retrieve the component list of equity index
# from Yahoo Finance, if available

import requests
p = 0

while p < 12:

    if p == 0:

        url = 'http://finance.yahoo.com/q/cp?s=%5E' + symbol
        text = requests.get(url).content
                                                                              # </a></b></td><td
        componentSubset = [text[n-10:n] for n in xrange(len(text)) if text.find('</a></b></td><td', n) == n]

        for comp in range(len(componentSubset)):

            componentSubset[comp] = componentSubset[comp][(1+componentSubset[comp].index('>')):]

        components = componentSubset

    else:

        url = 'http://finance.yahoo.com/q/cp?s=%5E' + symbol + '&c=' + str(p)
        text = requests.get(url).content

        componentSubset = [text[n-10:n] for n in xrange(len(text)) if text.find('</a></b></td><td', n) == n]

        for comp in range(len(componentSubset)):

            componentSubset[comp] = componentSubset[comp][(1+componentSubset[comp].index('>')):]

        components.extend(componentSubset)

    p = p + 1

components = set(components)

return components

似乎有效

getIndexComponents('FTSE')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM