资讯动态

yfinance 的 Sector 和 Industry 如何用 region 参数限定非美区数据

发布时间:2026/9/13 7:23:52 来源:尧图企业网站定制
yfinance 的 Sector 和 Industry 如何用 region 参数限定非美区数据【免费下载链接】yfinanceDownload market data from Yahoo! Finances API项目地址: https://gitcode.com/GitHub_Trending/yf/yfinanceyfinance 的Sector和Industry对象默认只返回美国U.S.市场的板块与行业数据。如果你要拿的是英国、德国、日本等非美区市场的头部公司、ETF 或基金列表需要在构造对象时传入region参数。这篇文章说明region参数的取值规则、它作用在哪些属性上以及如何验证限定确实生效。适用前提yfinance 从 1.4.0 起支持对Sector和Industry做 region 限定见 CHANGELOG.rst 中 “Allow region scoping for Sector and Industry (closes #2601) #2803” 条目。安装方式参见 安装文档本文场景下只需确保版本不低于 1.4.0。region 参数的取值与处理规则Sector和Industry的构造函数都接受region参数默认值为US见 yfinance/domain/sector.py 和 yfinance/domain/industry.py取值为 Yahoo region即 ISO 3166-1 alpha-2 国家代码例如US、GB、FR、DE、JP大小写不敏感。内部会先strip()再upper()所以us、 GB 、Fr都会被规范化为US、GB、FR规范化逻辑在 yfinance/domain/domain.pyself._region: str region.strip().upper()实际请求 Yahoo API 时该值会作为region请求参数发出与formattedtrue、withReturnstrue、langen-US一起提交见 yfinance/domain/domain.py 的_fetch方法。region限定作用于以下属性依据 文档说明对象受 region 限定的属性Sectortop_companies、top_etfs、top_mutual_fundsIndustrytop_companies公共属性、top_performing_companies、top_growth_companies不传region时这些属性返回的是 U.S. 数据。按 region 取数最小可用代码官方示例文档 给出的用法是import yfinance as yf yf.Sector(technology, regionGB).top_companies # UK yf.Sector(technology, regionDE).top_companies # Germany yf.Industry(software-infrastructure, regionJP) # Japan构造后按需访问属性即可例如import yfinance as yf tech yf.Sector(technology, regionDE) tech.name # 板块名称 tech.overview # 概览信息 tech.top_companies # 德国区头部公司DataFrame索引为 symbol tech.top_etfs # 头部 ETFDict[symbol, name] tech.top_mutual_funds # 头部共同基金Dict[symbol, name]Industry同理import yfinance as yf software yf.Industry(software-infrastructure, regionJP) software.top_performing_companies # 日本区表现最好的公司DataFrame software.top_growth_companies # 日本区成长性最好的公司DataFrame关于keytechnology、software-infrastructure这类键从哪来可以从 Ticker 的info里取sectorKey/industryKey反向定位官方示例 sector_industry_ticker.py 展示了这条链路import yfinance as yf msft yf.Ticker(MSFT) tech yf.Sector(msft.info.get(sectorKey)) software yf.Industry(msft.info.get(industryKey))其余公共属性key、name、symbol、ticker、research_reports与无 region 版本一致见 sector_industry.py。验证 region 限定是否生效仓库自带了针对该行为的测试 tests/test_sector_region.py其中展示了两种可直接复用的核对方式检查内部 region 值是否被正确规范化import yfinance as yf for raw, expected in [(us, US), ( GB , GB), (Fr, FR)]: s yf.Sector(technology, regionraw) assert s._region expected测试断言的规范化结果us→US、 GB →GB、Fr→FR。对比两个 region 的top_companies内容差异测试用“英国上市 symbol 带.L后缀、美国 symbol 不带”这一特征来判断取数确实来自不同市场import yfinance as yf us yf.Sector(technology).top_companies gb yf.Sector(technology, regionGB).top_companies assert us is not None assert gb is not None # UK-listed symbols carry the .L suffix, U.S. symbols do not. assert any(sym.endswith(.L) for sym in gb.index) assert not any(sym.endswith(.L) for sym in us.index)也就是说拿到结果后看top_companies的 indexsymbol 列英国区应出现带.L后缀的交易所代码与默认的美股列表形成可区分的差异。Industry的 region 是否传入也可以同样用ind._region断言测试中yf.Industry(software-infrastructure, regionDE)._region DE。边界与注意事项默认仍是美国忘记传region不会报错静默返回 U.S. 数据取数后建议核对 symbol 是否符合预期市场。lang 固定为en-US请求参数中的语言固定为英文只有region可随构造函数调整。作用范围以文档列举为准文档明确region限定top_companies、top_etfs、top_mutual_funds以及 industry 的 top performing/growth 列表Sector.industries等未列入限定范围说明的属性按默认行为对待即可。取数失败的处理与其余 Domain 行为一致若接口请求或解析失败在YfConfig.debug.hide_exceptions为 True 时记录 error 日志而不是直接抛出异常见 yfinance/domain/sector.py 的_fetch_and_parse。完成核对后如果需要进一步取到具体股票的行情可以继续沿用示例中的tech.ticker/software.ticker拿到Ticker对象再调用.info、.history()等常规接口见 sector_industry_ticker.py。【免费下载链接】yfinanceDownload market data from Yahoo! Finances API项目地址: https://gitcode.com/GitHub_Trending/yf/yfinance创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价