python - 如何用 python 转义这个 AJAX 网页?

标签 python ajax web-scraping beautifulsoup python-requests

我正在尝试抓取 Fantasy Football League website 上的播放器表 使用 python,但我有点菜鸟。

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

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


  1. 播放器
  2. 姓名
  3. 费用
  4. 卖出
  5. 表格
  6. 积分

旁注:我看到很多人在 reddit 上谈论这个“API”,所以它显然是可能的 - 我只是愚蠢地弄清楚如何复制网站发出的 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 名玩家的相关信息。

关于python - 如何用 python 转义这个 AJAX 网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718499/

相关文章:

javascript - 提交表格后停留在同一页面

javascript - 浏览器什么时候执行Javascript?执行光标如何移动?

php - 按预期打印数组中不同的值

python - 传入文件的正则表达式语句

python - 在 python 中使用 imageio 从图像制作 gif

python - Pandas 在区间中寻找值(value)

python - scrapy没有给出任何输出

python - 从 for 循环中提取特定的迭代输出

python - 当我在构建机器人时尝试包含登录按钮的 HTML 详细信息时,收到 "NoSuchElementException"

python - 判断对象是否已经完成