gpt4 book ai didi

python - 使用 Python 识别 ETF 持有量

转载 作者:行者123 更新时间:2023-12-05 02:03:48 24 4
gpt4 key购买 nike

我想创建一个网络抓取工具来收集 ETF 的特定持有量。我发现 Zacks.com 创建了一个很好的列表,列出了我正在寻找的东西。我正在尝试使用 BeautifulSoup,但是我很难在“符号”列中精确定位数据。我需要更改或添加什么才能将所有符号收集为列表?

import requests
from bs4 import BeautifulSoup

tickers = ["XLU","XLRE"] #list of tickers whose financial data needs to be extracted
financial_dir = {}


for ticker in tickers:
#getting holdings data from Zacks for the given ticker
temp_dir = {}
url = 'https://www.zacks.com/funds/etf/'+ticker+'/holding'
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content,'html.parser')
tabl = soup.find_all("table", {"id" : "etf_holding_table"})
for t in tabl:
rows = t.find_all("button")

最佳答案

import requests
import re

keys = ['XLU', 'XLRE']


headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"
}


def main(url):
with requests.Session() as req:
req.headers.update(headers)
for key in keys:
r = req.get(url.format(key))
print(f"Extracting: {r.url}")
goal = re.findall(r'etf\\\/(.*?)\\', r.text)
print(goal)


main("https://www.zacks.com/funds/etf/{}/holding")

输出:

Extracting: https://www.zacks.com/funds/etf/XLU/holding
['NEE', 'DUK', 'D', 'SO', 'AEP', 'XEL', 'EXC', 'SRE', 'WEC', 'ES', 'PEG', 'AWK', 'ED', 'DTE', 'PPL', 'ETR', 'AEE', 'EIX', 'CMS', 'FE', 'LNT', 'AES', 'ATO', 'EVRG', 'CNP', 'PNW', 'NI', 'NRG']
Extracting: https://www.zacks.com/funds/etf/XLRE/holding
['AMT', 'PLD', 'CCI', 'EQIX', 'DLR', 'PSA', 'SBAC', 'WELL', 'AVB', 'O', 'WY', 'SPG', 'ARE', 'EQR', 'VTR', 'CBRE', 'PEAK', 'EXR', 'DRE', 'MAA', 'ESS', 'BXP', 'UDR', 'HST', 'IRM', 'REG', 'VNO', 'AIV', 'FRT', 'KIM', 'SLG']

关于python - 使用 Python 识别 ETF 持有量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64908086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com