You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python请求完整导入Every Angle的OData数据集?

解决OData接口仅返回前3000行数据的问题

OData接口默认会对返回结果做分页处理,你当前只请求了一次接口,所以仅拿到第一页的3000行数据。要获取完整数据集,需要循环请求后续分页的链接,直到没有下一页为止。

原因分析

Every Angle的OData接口采用分页机制限制单次返回的数据量,响应结果中会包含@odata.nextLink字段(或XML格式的对应节点),指向后续分页的请求URL。原代码只执行了一次GET请求,自然只能获取第一页数据。

修正后的代码(JSON格式响应)

假设接口返回JSON格式数据,以下是支持自动分页的代码:

import requests
import pandas as pd

all_records = []
current_url = 'https://everyangle.fabory.com/odata/R3P_300/odata/Stock_MS30_LABST_LBKUM_L2_6394_30806/?$format=data'

while current_url:
    response = requests.get(current_url)
    print(f"请求状态码: {response.status_code}")
    if response.status_code != 200:
        break
    
    # 解析JSON响应
    data = response.json()
    # 将当前页数据追加到总列表
    all_records.extend(data.get('value', []))
    # 获取下一页链接
    current_url = data.get('@odata.nextLink')

# 转换为DataFrame,方便后续导入PowerBI
full_df = pd.DataFrame(all_records)
print(f"完整数据集记录数: {len(full_df)}")

# 可选:保存为CSV,直接导入PowerBI
# full_df.to_csv('full_stock_data.csv', index=False)

若接口返回XML格式的处理方式

如果接口返回XML结构,可改用XML解析逻辑:

import requests
import pandas as pd
import xml.etree.ElementTree as ET

all_records = []
current_url = 'https://everyangle.fabory.com/odata/R3P_300/odata/Stock_MS30_LABST_LBKUM_L2_6394_30806/?$format=data'

# OData XML命名空间,根据实际响应调整
namespaces = {
    'atom': 'http://www.w3.org/2005/Atom',
    'odata': 'http://docs.oasis-open.org/odata/ns/metadata',
    'edm': 'http://docs.oasis-open.org/odata/ns/data'
}

while current_url:
    response = requests.get(current_url)
    print(f"请求状态码: {response.status_code}")
    if response.status_code != 200:
        break
    
    # 解析XML响应
    root = ET.fromstring(response.content)
    
    # 提取当前页所有记录
    entries = root.findall('.//atom:entry', namespaces)
    for entry in entries:
        row = {}
        # 提取每个属性的键值对
        properties = entry.findall('.//edm:property', namespaces)
        for prop in properties:
            row[prop.get('name')] = prop.text
        all_records.append(row)
    
    # 获取下一页链接
    next_link = root.find('.//odata:nextLink', namespaces)
    current_url = next_link.text if next_link is not None else None

# 转换为DataFrame
full_df = pd.DataFrame(all_records)
print(f"完整数据集记录数: {len(full_df)}")

额外说明

  1. 原代码中URL的引号未闭合(末尾缺少'),需先修正这个语法错误。
  2. 若$format=data格式不支持分页链接返回,可尝试改用$format=json或$format=xml,确保接口返回包含分页标识的结构。
  3. 将生成的full_df保存为CSV或直接写入数据库,再导入PowerBI即可获取完整数据集。

内容的提问来源于stack exchange,提问作者HeadOverFeet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:25:19