0

我想获得共同基金的投资组合。我可以在 MorningStar 上看到它。(https://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F00000ZG4Z&tab=3

在开发工具中,我找到了 API,但我只能得到 Response 401 - 未授权。

这里的代码:

import requests

url = 'https://www.us-api.morningstar.com/sal/sal-service/fund/portfolio/holding/v2/F00000ZG4Z/data'

params = {
'premiumNum': '10',
'freeNum': '10',
'languageId': 'en-GB',
'locale': 'en-GB',
'clientId': 'MDC_intl',
'benchmarkId': 'category',
'component': 'sal-components-mip-holdings',
'version': '3.40.1'
}

headers = {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
'authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI3RUI4NDcyRi1FRjk4LTQ0ODgtQjJGQS0zQzMyQUQyQjdCNUYiLCJjdXN0b206ZGF0YV9yb2xlIjpbIlFTLk1hcmtldHMiLCJRUy5QdWxscXMiLCJTQUwuU2VydmljZSJdLCJjdXN0b206dWltX3JvbGVzIjoiIiwiY3VzdG9tOmNvbmZpZ19pZCI6IkVVUlRMX0VDIiwiaXNzIjoiVUlNIiwiZXhwIjoxNjEyNzkzNzgwLCJjdXN0b206cm9sZSI6WyJFQy5TZXJ2aWNlLkNvbmZpZ3VyYXRpb24iLCJFQy5TZXJ2aWNlLkhvc3RpbmciXSwiaWF0IjoxNjEyNzkwMTgwLCJjdXN0b206Y29tcGFueV9pZCI6IjUzNGJiMTM0LTMwNWYtNDhhYi04MmYxLWNjZTBlZDFlYTgzYSIsImN1c3RvbTppbnRlcm5hbF9jb21wYW55X2lkIjoiQ2xpZW50MCJ9.rVnAGGBsBxv3XMnBkAUJdo9tJmrsBGqjj2Wxtz0CLdz7MzErej8RBbVfDOEd1xdxmSd5YpAxFA_kuu41r8jyhCTAAvtBE6gWzizXMKxbZ7RQ9hxnWm3dVnsHspWKCwCCQl5B2WpBjDbTH-edBHmTLuUrq9Qc3_2r8Lx28-oFbxXtzdmlSssgtZqdz2dMXN-Fo8LYMrTPtqt0Kv_lwQMmH42_PwZd6BqtzMvpqN1rzCHRFN-y5xL4yZ_PGBviuhD841mXVNCWnv8WGYHOK5p0JEJ7pg17O2fvEhuMxrpwvmC4LRELsA-QRgCo15UpOp_AHcxg_r7HPxPA9TtHZUy6Rw',
'credentials': 'omit',
'origin': 'https://www.morningstar.co.uk',
'referer': 'https://www.morningstar.co.uk/',
'sec-ch-ua': '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
'x-api-requestid': '6c20fb56-8055-aa34-1632-5f5a8dbaddde',
'x-sal-contenttype': 'e7FDDltrTy+tA2HnLovvGL0LFMwT+KkEptGju5wXVTU='
}

response = requests.get(url, params=params, headers=headers)

print(response)

你知道我怎样才能得到回应吗?

谢谢你

4

2 回答 2

2

Mael,看起来您确实需要更新 beerer 令牌。只是在玩这个网站,它看起来<script>来自https://www.morningstar.fr/Common/funds/snapshot/PortfolioSAL.aspx.

所以你可以做的是在那里做一个请求,然后使用 BeautifulSoup 提取令牌,然后将它提供给你的第二个请求标头。

import requests
from bs4 import BeautifulSoup

# Get the Bearer Token
url = 'https://www.morningstar.fr/Common/funds/snapshot/PortfolioSAL.aspx'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'}
payload = {
'Site': 'fr',
'FC': 'F000010S65',
'IT': 'FO',
'LANG': 'fr-FR'}
response = requests.get(url, headers=headers, params=payload)
soup = BeautifulSoup(response.text, 'html.parser')
script = soup.find('script', {'type':'text/javascript'})
bearerToken = str(script).split('tokenMaaS:')[-1].split('}')[0].replace('"','').strip()



# API request
url = 'https://www.us-api.morningstar.com/sal/sal-service/fund/portfolio/holding/v2/F00000ZG4Z/data'

params = {
'premiumNum': '10',
'freeNum': '10',
'languageId': 'en-GB',
'locale': 'en-GB',
'clientId': 'MDC_intl',
'benchmarkId': 'category',
'component': 'sal-components-mip-holdings',
'version': '3.40.1'
}

headers = {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
'authorization': 'Bearer %s' %bearerToken,
'credentials': 'omit',
'origin': 'https://www.morningstar.co.uk',
'referer': 'https://www.morningstar.co.uk/',
'sec-ch-ua': '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
'x-api-requestid': '6c20fb56-8055-aa34-1632-5f5a8dbaddde',
'x-sal-contenttype': 'e7FDDltrTy+tA2HnLovvGL0LFMwT+KkEptGju5wXVTU='
}

response = requests.get(url, params=params, headers=headers).json()

print(response)

输出:

print(response)
{'masterPortfolioId': '1405909', 'secId': 'F00000ZG4Z', 'baseCurrencyId': 'EUR', 'domicileCountryId': 'LUX', 'numberOfHolding': 86, 'numberOfEquityHolding': 70, 'numberOfBondHolding': 0, 'numberOfOtherHolding': 16, 'assetType': 'EQUITY', 'holdingSummary': {'portfolioDate': '2020-12-31T06:00:00.000', 'topHoldingWeighting': 37.75262, 'equityNumberOfHolding': 70, 'fixedIncomeNumberOfHolding': 0, 'numberOfHolding': 86, 'numberOfOtherHolding': 16, 'lastTurnover': 99.53, 'LastTurnoverDate': '2020-09-30T05:00:00.000', 'secId': 'F00000ZG4Z', 'averageTurnoverRatio': 71.656}, 'holdingActiveShare': {'activeShareValue': None, 'activeShareDate': None, 'etfBenchmarkProxyName': None}, 'equityHoldingPage': {'pageSize': 10, 'pageNumber': 1, 'totalPage': 1, 'numberOfCurPageHolding': 8, 'numberOfAllHolding': 8, 'holdingList': [{'securityName': 'Roku Inc Class A', 'secId': '0P0001BMW4', 'performanceId': '0P0001BMW4', 'holdingTypeId': 'E', 'weighting': 6.49644, 'numberOfShare': 1469433.0, 'marketValue': 406810852.0, 'shareChange': 62826.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 269.7634, 'forwardPERatio': -1111.1111, 'stockRating': '1', 'economicMoat': 'None  ', 'sector': 'Communication Services', 'sectorCode': 'communicationsServices', 'holdingTrend': {'trend': [197984504.0, 236416508.0, 323771119.0, 406810852.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US77543R1023', 'cusip': '77543R102', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2019-03-31T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'Tesla Inc', 'secId': '0P0000OQN8', 'performanceId': '0P0000OQN8', 'holdingTypeId': 'E', 'weighting': 4.86194, 'numberOfShare': 536170.0, 'marketValue': 304457190.0, 'shareChange': -219182.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 437.7551, 'forwardPERatio': 175.4386, 'stockRating': '1', 'economicMoat': 'Narrow', 'sector': 'Consumer Cyclical', 'sectorCode': 'consumerCyclical', 'holdingTrend': {'trend': [237157107.0, 239331502.0, 369883797.0, 304457190.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US88160R1014', 'cusip': '88160R101', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2017-04-30T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'Amazon.Com Inc', 'secId': '0P000000B7', 'performanceId': '0P000000B7', 'holdingTypeId': 'E', 'weighting': 3.8275, 'numberOfShare': 89250.0, 'marketValue': 239680015.0, 'shareChange': 48494.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 67.0147, 'forwardPERatio': 61.3497, 'stockRating': '4', 'economicMoat': 'Wide  ', 'sector': 'Consumer Cyclical', 'sectorCode': 'consumerCyclical', 'holdingTrend': {'trend': [44094916.0, 39876647.0, 108867835.0, 239680015.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US0231351067', 'cusip': '023135106', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2017-04-30T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'Snap Inc Class A', 'secId': '0P00019RU1', 'performanceId': '0P00019RU1', 'holdingTypeId': 'E', 'weighting': 3.80126, 'numberOfShare': 5901749.0, 'marketValue': 238036797.0, 'shareChange': -920856.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 370.5011, 'forwardPERatio': 625.0, 'stockRating': '3', 'economicMoat': 'None  ', 'sector': 'Communication Services', 'sectorCode': 'communicationsServices', 'holdingTrend': {'trend': [126247327.0, 222153351.0, 262534930.0, 238036797.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US83304A1060', 'cusip': '83304A106', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2017-05-31T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'Apple Inc', 'secId': '0P000000GY', 'performanceId': '0P000000GY', 'holdingTypeId': 'E', 'weighting': 3.72578, 'numberOfShare': 2134823.0, 'marketValue': 233310589.0, 'shareChange': 2134823.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 88.1914, 'forwardPERatio': 29.8507, 'stockRating': '2', 'economicMoat': 'Narrow', 'sector': 'Technology', 'sectorCode': 'technology', 'holdingTrend': {'trend': [233310589.0, None, None, None], 'period': ['2020-12-31T06:00:00.000', None, None, None]}, 'holdingType': 'Equity', 'isin': 'US0378331005', 'cusip': '037833100', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2020-12-31T06:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'The Walt Disney Co', 'secId': '0P000005UJ', 'performanceId': '0P000005UJ', 'holdingTypeId': 'E', 'weighting': 3.34656, 'numberOfShare': 1415315.0, 'marketValue': 209563495.0, 'shareChange': 127680.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 65.7289, 'forwardPERatio': 156.25, 'stockRating': '2', 'economicMoat': 'Wide  ', 'sector': 'Communication Services', 'sectorCode': 'communicationsServices', 'holdingTrend': {'trend': [102933315.0, 102698905.0, 158376195.0, 209563495.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US2546871060', 'cusip': '254687106', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2020-04-30T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'General Electric Co', 'secId': '0P000002DO', 'performanceId': '0P000002DO', 'holdingTypeId': 'E', 'weighting': 3.25068, 'numberOfShare': 23255521.0, 'marketValue': 203559663.0, 'shareChange': 3458461.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 20.864, 'forwardPERatio': 52.356, 'stockRating': '3', 'economicMoat': 'Narrow', 'sector': 'Industrials', 'sectorCode': 'industrials', 'holdingTrend': {'trend': [78360111.0, 102428521.0, 172119436.0, 203559663.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US3696041033', 'cusip': '369604103', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2020-05-31T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'Broadcom Inc', 'secId': '0P0000KU35', 'performanceId': '0P0000KU35', 'holdingTypeId': 'E', 'weighting': 2.98084, 'numberOfShare': 525242.0, 'marketValue': 186661707.0, 'shareChange': 120564.0, 'country': 'United States', 'ticker': None, 'totalReturn1Year': 84.4802, 'forwardPERatio': 18.622, 'stockRating': '1', 'economicMoat': 'Narrow', 'sector': 'Technology', 'sectorCode': 'technology', 'holdingTrend': {'trend': [110608766.0, 102402300.0, 133612668.0, 186661707.0], 'period': ['2020-09-30T05:00:00.000', '2020-10-31T05:00:00.000', '2020-11-30T06:00:00.000', '2020-12-31T06:00:00.000']}, 'holdingType': 'Equity', 'isin': 'US11135F1012', 'cusip': '11135F101', 'secondarySectorId': None, 'superSectorName': None, 'primarySectorName': None, 'secondarySectorName': None, 'firstBoughtDate': '2018-04-30T05:00:00.000', 'maturityDate': None, 'coupon': None, 'currency': 'US Dollar', 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': None, 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}]}, 'boldHoldingPage': {'pageSize': 10, 'pageNumber': 1, 'totalPage': 0, 'numberOfCurPageHolding': 0, 'numberOfAllHolding': 0, 'holdingList': []}, 'otherHoldingPage': {'pageSize': 10, 'pageNumber': 1, 'totalPage': 1, 'numberOfCurPageHolding': 2, 'numberOfAllHolding': 2, 'holdingList': [{'securityName': 'Cost Of Currency (Cash) 912000000Usd 912000000Usd', 'secId': None, 'performanceId': None, 'holdingTypeId': 'C', 'weighting': 3.42506, 'numberOfShare': 262427188.0, 'marketValue': 214479116.0, 'shareChange': 262427188.0, 'country': None, 'ticker': None, 'totalReturn1Year': None, 'forwardPERatio': None, 'stockRating': None, 'economicMoat': None, 'sector': None, 'sectorCode': None, 'holdingTrend': None, 'holdingType': 'Other', 'isin': None, 'cusip': None, 'secondarySectorId': '501011', 'superSectorName': 'cashAndEquivalents', 'primarySectorName': 'cashAndEquivalents', 'secondarySectorName': 'Cash', 'firstBoughtDate': None, 'maturityDate': None, 'coupon': None, 'currency': None, 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': '_PO_', 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}, {'securityName': 'Receivable For Fund Shares Sold 914100000Usd1 914100000Usd1', 'secId': None, 'performanceId': None, 'holdingTypeId': 'CD', 'weighting': 3.10513, 'numberOfShare': 237914432.0, 'marketValue': 194445086.0, 'shareChange': 237914432.0, 'country': None, 'ticker': None, 'totalReturn1Year': None, 'forwardPERatio': None, 'stockRating': None, 'economicMoat': None, 'sector': None, 'sectorCode': None, 'holdingTrend': None, 'holdingType': 'Other', 'isin': None, 'cusip': None, 'secondarySectorId': '501023', 'superSectorName': 'cashAndEquivalents', 'primarySectorName': 'cashAndEquivalents', 'secondarySectorName': 'TD/CD', 'firstBoughtDate': None, 'maturityDate': None, 'coupon': None, 'currency': None, 'prospectusNetExpenseRatio': None, 'oneYearReturn': None, 'morningstarRating': None, 'ePUsedForOverallRating': 0, 'analystRating': '_PO_', 'totalAssets': None, 'ttmYield': None, 'epUsedFor1YearReturn': 0, 'morningstarCategory': None, 'totalAssetsMagnitude': None, 'lastTurnoverRatio': None}]}, 'userType': 'Free', 'portfolioLastestDateFooter': '2020-12-31T06:00:00.000', 'noPremiumChinaFund': False}
于 2021-03-02T09:21:05.880 回答
0

问题是您需要提供更新的 Bearer 令牌,因为它过期了。您是否监控网络流量以了解如何在每次请求之前获取有效令牌?

于 2021-02-17T13:18:46.283 回答