python - 我不明白 JSON 在这个网站上是如何工作的
问题描述
我有两个问题:
- 我的目标是从这个示例网站(或其他现场篮球比赛)中抓取实时数据:
当我打开开发工具并从请求中浏览 JSON 时:
https://fb.oddsportal.com/feed/live/1-3-zVIgebsh-2-1-yjc50.dat?_=1573397634508
我看到了更多数据,但我不知道该怎么做。例如上一页:
https://www.oddsportal.com/inplay-odds/live-now/basketball/
我有类似的表,但我确实得到了结果:
2.24 - 1.58 - Filippos Veroi - AS Karditsas
1.26 - 3.56 - 拜仁 - 阿尔巴柏林
1.24 - 4.21 - 拜罗伊特 - 篮球布伦瑞克
2.2 - 1.75 - 哥廷根 - 奥尔登堡
1.01 - 12.43 - 多纳尔格罗宁根 - 鹿特丹
通过此代码:
import json
import time
import pprint
import re
import sys
import requests
from bs4 import BeautifulSoup
url = "https://fb.oddsportal.com/feed/livegames/live/3/0.dat?_{}".format(int(time.time() * 1000))
headers = {
'User-Agent': 'curl/7.64.0',
'Referer': 'https://www.oddsportal.com/inplay-odds/live-now/basketball/',
}
page = requests.get(url, headers=headers)
result = re.search("globals.jsonpCallback\('/feed/livegames/live/3/0.dat', (.*)\);", page.text)
if not result:
sys.exit("Result not found")
#print(result.group(1))
json_text = result.group(1)
json_data = json.loads(json_text).get('d')
soup = BeautifulSoup(json_data.get('text', ''), 'html.parser')
with open('test.html', 'w+') as f:
f.write(str(soup))
for m in soup.select('tr[xeid]'):
name = m.find('td', class_='name table-participant').text
#print (len(name))
#ids = m.find_all('td', class_='live odds-nowrp')
xoid, xodd = (s['xoid'].split('-')[-1] for s in m.find_all('td', class_='live odds-nowrp'))
xoid_data = json_data.get('odds', {}).get(xoid)
xodd_data = json_data.get('odds', {}).get(xodd)
if xoid_data is not None:
xoid_data = xoid_data[1]
if xodd_data is not None:
xodd_data = xodd_data[1]
print(u'{} - {} - {}'.format(xoid_data, xodd_data, name))
但是在上一页 JSON 是不同的。我从“text”中提取 id 并使用“odds”来获取数据,但在这里我看到了许多让我感到困惑的“odds”。
JSON如何在下一页工作?如何获得与上一页类似的结果?(例如:
大/小 +155.5 - 3.06 - 1.31
大/小 +156.5 - 2.82 - 1.35
大/小 +157.5 - 2.61 - 1.41
大/小 +158.5 - 2.43 - 1.47
大/小 +159.5 - 2.27 - 1.54
大/小 +160.5 - 2.12 - 1.61
大/小 +161.5 - 2.00 - 1.67
我对下载文本很感兴趣,比如“1st Quarter 7”(2cm up table)。我怀疑答案在 JSON 中:
https://fb.oddsportal.com/feed/postmatchscore/3-MTkEXywD-yj837.dat?_=1573399835075
但类似于第一个问题,我不明白 JSON 在这个网站上是如何工作的
提前感谢您的帮助。
解决方案
推荐阅读
- r - 将几条水平线添加到小型多图
- google-cloud-platform - Private Google Access 是否也用于具有公共 IP 或使用 Cloud NAT 的实例?
- c - 使用 PAPI 从 perf_event_uncore 列表中读取硬件计数器
- arrays - 在 2 列中查找行,其中 IF AND 在相应行中的这 2 个单元格之间相遇
- java - 在哪里可以找到每个 JDK 版本的最低兼容 JRE 版本是什么?
- java-8 - 从列表中提取两个大小相同的对象
- javascript - webgl2传递属性含义
- javascript - 将多个 Node.js 应用程序限制为具有多个 IPv4 地址的机器上的特定 IP 地址
- api - 如何使用 Microsoft Graph API 从 Microsoft Teams Live 活动会话中导出与会者列表?
- powershell - 如何解析来自 Invoke-WebRequest 的响应