2017-10-10 62 views
0

這是我第一次在這裏發帖,所以請原諒我,如果有類似的問題。我有一個csv文件,裏面有大約150個股票代碼,我想把每個文件都放到一個Morningstar的url中,下載一個帶有該公司損益表的csv。任何人都可以指出我在這之後去哪個方向?正在下載財務報表

import pandas as pd 
import requests 
from bs4 import BeautifulSoup as soup 
import csv 

df_1 = pd.read_csv('tickers1.csv',delimiter=',') 

for i in range(len(df_1)): 
    url1 = 'https://financials.morningstar.com/ajax/Report Process4CSV.html?t=%i&reportType=is&period=12&dataType=A&order=asc&columnYear=5&number=3' %(i) 

從這裏,我不知道該怎麼辦。有沒有更有意義的方法來做到這一點?或者我在這裏的正確軌道上?我對Python很新。

+0

我不確定這是適合這類問題的地方。這不是關於特定的編程問題。相反,這是一個要求解釋一個主題,指向外部資源,或實施一個沒有明確規定的解決方案。 –

+0

歡迎來到StackOverflow。請閱讀[如何提問](https://stackoverflow.com/help/how-to-ask),告訴我們你已經嘗試了什麼,以及它爲什麼可以工作或不工作。 – Antimony

+0

閱讀['urllib'](https://docs.python.org/3/library/urllib.request.html#examples),它很直觀 –

回答

0

我認爲這是一個有趣的問題,所以我嘗試了一些想法,但我無法在Python中獲得任何工作。我想我不太瞭解Python,但我確信你可以做到!無論如何,我知道Excel非常好,所以我使用VBA & Excel爲您制定了一個解決方案。

Sub HTML_Table_To_Excel() 

Dim htm As Object 
Dim Tr As Object 
Dim Td As Object 
Dim Tab1 As Object 


'Replace the URL of the webpage that you want to download 
Web_URL = "http://www.advfn.com/stock-market/NASDAQ/MSFT/financials?btn=annual_reports&mode=company_data" 

'Create HTMLFile Object 
Set HTML_Content = CreateObject("htmlfile") 

'Get the WebPage Content to HTMLFile Object 
With CreateObject("msxml2.xmlhttp") 
.Open "GET", Web_URL, False 
.send 
HTML_Content.body.innerHTML = .responseText 'this is the highlighted part for the error 
End With 

Column_Num_To_Start = 1 
iRow = 1 
iCol = Column_Num_To_Start 
iTable = 0 

    'Loop Through Each Table and Download it to Excel in Proper Format 
    For Each Tab1 In HTML_Content.getElementsByTagName("table") 

     If iTable > 2 And iTable < 6 Then 
      With HTML_Content.getElementsByTagName("table")(iTable) 
      For Each Tr In .Rows 
      For Each Td In Tr.Cells 
      Sheets(1).Cells(iRow, iCol).Select 
      Sheets(1).Cells(iRow, iCol) = Td.innerText 
      iCol = iCol + 1 
      Next Td 
      iCol = Column_Num_To_Start 
      iRow = iRow + 1 
      Next Tr 
      End With 

      iCol = Column_Num_To_Start 
      iRow = iRow + 1 
     End If 

     Debug.Print iTable 
     iTable = iTable + 1 
    Next Tab1 

MsgBox "Process Completed" 
End Sub 

顯然,這不是一個Python的解決方案,但希望你和你的要求來使用它不夠靈活,而且我沒有看到任何其他的解決方案在這裏反正。如果這對您有用,我可以添加一個循環來移動多個股票代碼,併爲每個代碼導入數據。我相信你可以使用Google財務來做到這一點。讓我知道您是否想要使用Google數據,並且我將爲您使用該數據源構建解決方案(它應該基本相同,但取決於如何定義某些財務指標可能略有不同)。