Yahoo梦幻足球API的Start参数对返回结果无影响的问题求助
Yahoo梦幻足球API的Start参数对返回结果无影响的问题求助
我现在的核心需求是通过Yahoo梦幻足球API拉取所有球员的数据。我知道Yahoo限制单次请求最多返回25个球员,之前了解到可以通过修改Start参数然后循环请求的方式来获取完整数据集,但实际测试下来发现这个方法行不通——不管我把Start设为0、25还是2500,返回的球员数据完全一模一样。
我构造请求URL的代码是这样的:
url = f"{self.base_url}/league/{self.league_id}/players;status=A;start={start},count=25"
以下是我的完整脚本,麻烦帮我排查下问题所在:
import os import requests from requests_oauthlib import OAuth2Session import webbrowser import xml.etree.ElementTree as ET import csv import json class YahooFantasyAPI: def __init__(self): self.client_id = os.getenv('API_KEY') self.client_secret = os.getenv('API_SECRET') self.redirect_uri = 'https://localhost/' self.league_id = os.getenv('league_id') self.base_url = 'https://fantasysports.yahooapis.com/fantasy/v2' self.token = None self.oauth = OAuth2Session(self.client_id, redirect_uri=self.redirect_uri, scope='openid') self.state = None self.item_dict = {} def authenticate(self): # Step 1: Get authorization URL and state authorization_url, self.state = self.oauth.authorization_url( 'https://api.login.yahoo.com/oauth2/request_auth' ) print(f"Please go to this URL and authorize access: {authorization_url}") webbrowser.open(authorization_url) # Step 2: User pastes the redirected URL with the code redirect_response = input("Paste the full redirect URL here: ") print(f"Redirect URL: {redirect_response}") # Step 3: Verify that the state parameter from the response matches the one in the request if self.state not in redirect_response: raise ValueError(f"State in the response does not match the original state. Response state: {redirect_response}") print(f"State during token request: {self.state}") # Step 4: Fetch the token, ensuring that the state is passed correctly self.token = self.oauth.fetch_token( 'https://api.login.yahoo.com/oauth2/get_token', authorization_response=redirect_response, client_secret=self.client_secret, state=self.state # Explicitly pass the state value ) print("Authentication successful!") def get_players(self): item_dict = {} if not self.token: raise ValueError("Authenticate first by calling the authenticate() method.") done = False start = 1 while(not done) : # Define the endpoint URL to get league players url = f"{self.base_url}/league/{self.league_id}/players;status=A;start={start},count=25" print(url) headers = { 'Authorization': f"Bearer {self.token['access_token']}", 'Accept': 'application/xml' # Expect XML response } response = self.oauth.get(url, params={'format': 'json'}) # Print the status code and response content to debug print(f"Response Status Code: {response.status_code}") print(f"Content-Type: {response.headers.get('Content-Type')}") # print(response.content) with open('all.json', 'a') as csvfile: csvfile.write(response.text) csvfile.close() item_dict = json.loads(response.text) numPlayersInResp=len(item_dict['fantasy_content']['league'][1]['players']) # print(item_dict) print(numPlayersInResp) start += 25 if numPlayersInResp < 25: done = True # Usage if __name__ == '__main__': yahoo_api = YahooFantasyAPI() yahoo_api.authenticate() yahoo_api.get_players()
备注:内容来源于stack exchange,提问作者Dazee maser
相关产品推荐
相关产品推荐

