如何使用python对AJAX网页进行转义?

我正在尝试使用python在 Fantasy Football League website 上刮取播放器表格,但我有点不满意。

我知道网站被称为此外部URL来检索播放器数据: https://fantasy.premierleague.com/api/bootstrap-static/

我的问题是,这几乎是一个未公开的API。您如何建议找出如何调用此URL找回:


  1. 玩家
  2. 名称
  3. 费用
  4. 出售
  5. 表格

旁注:我看到很多人在reddit上谈论这个“ API”,所以这显然是可能的-我只是愚蠢地弄清楚如何复制该站点进行的AJAX调用。

非常感谢!

fluid120 回答:如何使用python对AJAX网页进行转义?

尝试一下:

import requests
import json
import pandas as pd

resp = requests.get('https://fantasy.premierleague.com/api/bootstrap-static/')
data = json.loads(resp.text)  #the response is in json format

players =[] #initialize a list of all players
for i in data['elements']:
    #the relevant info is hidden in here
    player = [] #initialize a list of relevant items for each player
    player.append(i['second_name'])
    cost = i['now_cost']/10
    player.append(cost)
    sel = float(i['selected_by_percent'])
    player.append(sel)
    player.append(i['form'])
    player.append(i['total_points'])
    players.append(player)

#now load the list into a dataframe
columns = ['Player','Cost','Sel.','Form','Pts.']
df = pd.DataFrame(players,columns=columns)
df

这应该为所有557名球员输出相关信息。

本文链接:https://www.f2er.com/3156796.html

大家都在问