首页 > 解决方案 > 我不明白 JSON 在这个网站上是如何工作的

问题描述

我有两个问题:

  1. 我的目标是从这个示例网站(或其他现场篮球比赛)中抓取实时数据:

https://www.oddsportal.com/basketball/greece/greek-cup/promitheas-panathinaikos-MTkEXywD/inplay-odds/#over-under;1

当我打开开发工具并从请求中浏览 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

  1. https://www.oddsportal.com/basketball/greece/greek-cup/promitheas-panathinaikos-MTkEXywD/inplay-odds/#over-under;1

我对下载文本很感兴趣,比如“1st Quarter 7”(2cm up table)。我怀疑答案在 JSON 中:

https://fb.oddsportal.com/feed/postmatchscore/3-MTkEXywD-yj837.dat?_=1573399835075

但类似于第一个问题,我不明白 JSON 在这个网站上是如何工作的

提前感谢您的帮助。

标签: pythonjsonweb-scraping

解决方案


推荐阅读