Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
交互式代理python API-将ReportsFinommary转换为pandas_Python_Beautifulsoup_Interactive Brokers_Ibpy_Ib Api - Fatal编程技术网

交互式代理python API-将ReportsFinommary转换为pandas

交互式代理python API-将ReportsFinommary转换为pandas,python,beautifulsoup,interactive-brokers,ibpy,ib-api,Python,Beautifulsoup,Interactive Brokers,Ibpy,Ib Api,我正在尝试做一些我认为很简单的事情。。。。显然不是。我想使用python ib api从ReportFinSummary中提取一些数据。我希望能够使用DPS数据。我试图用漂亮的汤对xml代码进行排序,但没有任何乐趣。任何帮助都将不胜感激。艾伦 from ib.opt import ibConnection, message from ib.ext.Contract import Contract from time import sleep from bs4 import BeautifulSo

我正在尝试做一些我认为很简单的事情。。。。显然不是。我想使用python ib api从ReportFinSummary中提取一些数据。我希望能够使用DPS数据。我试图用漂亮的汤对xml代码进行排序,但没有任何乐趣。任何帮助都将不胜感激。艾伦

from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep
from bs4 import BeautifulSoup
import pandas as pd

def fundamentalData_handler(msg):
    print(msg)

def error_handler(msg):
    print(msg)

tws = ibConnection(port=7497, clientId=123)
tws.register(error_handler, message.Error)
tws.register(fundamentalData_handler, message.fundamentalData)
tws.connect()

c = Contract()
c.m_symbol = 'RDSA'
c.m_secType = 'STK'
c.m_exchange = "SMART"
c.m_currency = "GBP"

tws.reqFundamentalData(1,c,'ReportsFinSummary')

soup = BeautifulSoup(tws.reqFundamentalData(1,c,'ReportsFinSummary'),'xml')
DPS_Data = soup.find_all('DividendPerShares')
DPS = []
for dates in DPS_Data:
    DPS.append(DPS_Data.get_text())

print(pd.DataFrame({'DPS_Data': DPS}))

sleep(2)

tws.disconnect()

这是未经测试,但应该让你在正确的轨道上。如果您使用的是多年未开发的IbPy,最好使用本机API或IbPythonic

from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep
from bs4 import BeautifulSoup
import pandas as pd

class IB():
    def __init__(self):
        pass

    def fundamentalData_handler(self, msg):
        self.data = msg

    def error_handler(self, msg):
        print(msg)

    def connect(self, port=7497, clientId=123):
        self.tws = ibConnection(port, clientId)
        self.tws.register(self.error_handler, message.Error)
        self.tws.register(self.fundamentalData_handler, message.fundamentalData)
        self.tws.connect()

    def disconnect(self):
        self.tws.disconnect()

    def get(self):
        c = Contract()
        c.m_symbol = 'RDSA'
        c.m_secType = 'STK'
        c.m_exchange = "SMART"
        c.m_currency = "GBP"

        self.tws.reqFundamentalData(1, c, 'ReportsFinSummary')
        while not hasattr(self, 'data'):
            sleep(0.1)

        soup = BeautifulSoup(self.data)
        DPS_Data = soup.find_all('DividendPerShares')
        DPS = [DPS_Data.get_text() for dates in DPS_Data]

        return pd.DataFrame({'DPS_Data': DPS})

ib = IB()
ib.connect()
df = ib.get()
ib.disconnect()
print(df)

嗨,我确实为我指明了正确的方向,不幸的是,两周后我仍然不在那里。我认为由于某种原因,xml报告没有存储在变量中,因此beautiful soup没有被存储在变量中。。。。美丽的如果您有任何关于此代码的提示,我们将不胜感激: