通过for循环获取DataFrame列表

问题描述 投票:0回答:1

使用Python,我想从互联网上爬取上市公司高管持有详细信息。因此,我首先创建名为 target_list_onlynumber 的完整股票代码列表(仅包含数字),如下所示:20240505 A

这是我尝试从中抓取数据的网站:https://data.eastmoney.com/executive/000001.html。它对应股票代码<000001>。对于代码为 i 的股票,对应的网站是 https://data.eastmoney.com/executive/i.html 通过执行以下代码,我可以获得我想要的 dataframe:

df = pd.DataFrame(
    requests.get('https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPT_EXECUTIVE_HOLD_DETAILS&columns=ALL&filter=(SECURITY_CODE%3D"000001")')\
        .json().get('result').get('data'))
print(df)

生成的数据框如下所示:20240505 B

现在我想编写一个for循环来获取其代码位于target_list_onlynumber列表中的所有股票的所有DataFrame。 df_i 是代码为 i 的股票的 DataFrame。 (例如,df_601857是代码为601857的股票的DataFrame。)这是我尝试的,与之前股票的代码类似<000001>:

df_list = []
for i in target_list_onlynumber:
    exec_data = requests.get('https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPT_EXECUTIVE_HOLD_DETAILS&columns=ALL&filter=(SECURITY_CODE%3D"{i}")')\
        .json().get('result').get('data')
    df_i = pd.DataFrame(exec_data)
    df_list.append(df_i)
    print(df_i)

但结果是:

Traceback (most recent call last):
  File "/tmp/jqcore/jqboson/jqboson/core/entry.py", line 379, in _run
    engine.start()
  File "/tmp/jqcore/jqboson/jqboson/core/engine.py", line 231, in start
    self._dispatcher.start()
  File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 280, in start
    self._run_loop()
  File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 240, in _run_loop
    self._loop.run()
  File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 107, in run
    self._handle_queue()
  File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 153, in _handle_queue
    message.callback(**message.callback_data)
  File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_subscriber.py", line 228, in broadcast
    consumer.send(market_data)
  File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 59, in   consumer_gen
    msg_callback()
  File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 52, in  msg_callback
    callback(market_data)
  File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 122, in wrapper
    result = callback(*args, **kwargs)
  File "/tmp/jqcore/jqboson/jqboson/core/strategy.py", line 474, in _wrapper
    self._context.current_dt
  File "/tmp/strategy/user_code.py", line 90, in handle_data
    .json().get('result').get('data')
AttributeError: 'NoneType' object has no attribute 'get'

我不明白为什么一只股票的代码成功而 for 循环失败。有人可以帮我解决吗?

python dataframe list get attributeerror
1个回答
0
投票

要将参数 I 传递到 URL 字符串的方式必须在字符串的开头使用“f”

而不是:

exec_data = requests.get('https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPT_EXECUTIVE_HOLD_DETAILS&columns=ALL&filter=(SECURITY_CODE%3D"{i}")')\
    .json().get('result').get('data')

做:

exec_data = requests.get(f'https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPT_EXECUTIVE_HOLD_DETAILS&columns=ALL&filter=(SECURITY_CODE%3D"{i}")')\
    .json().get('result').get('data')

告诉 Python 使用 stile 格式,用它的值替换大括号中的内容

© www.soinside.com 2019 - 2024. All rights reserved.