gpt4 book ai didi

python - 使用 Python 从 oddsportal.com 抓取数据

转载 作者:行者123 更新时间:2023-12-05 08:49:11 25 4
gpt4 key购买 nike

我一直在尝试从这个 ajax website 中提取数字上每个单元格的数据, 每个单元格的详细信息只有当鼠标指向单元格时才会弹出。

我已使用 Python selenium webdriver 和 phantomjs 加载和提取 page_source,但未找到数据。我使用 firebug 查找可能从中加载内容的任何 .json 文件,但没有找到。

请查看该网站,并建议我如何从指向 map 上每个单元格时移动的悬停框中的内容中抓取内容。

P.S:我在堆栈溢出和几个网站上做了很多研究,但都无济于事。

最佳答案

数据随手可得,您所需要做的就是好好看看幕后发生的事情。如何?使用 Developer Tool 并检查请求。您肯定会注意到有两个满足您的需求:

第一个以秒为单位使用当前时间,另一个以毫秒为单位。每次发出请求时都必须交换它。

bookies 请求是该站点所有 bookies 的映射。这就是我如何获得 1xBet 代码,即 417。您可以轻松地将其与赔率数据进行映射,例如,以获取给定博彩公司和/或博彩公司的所有历史投注。这里有很多可能性。

赔率请求就是您在这些表格中看到的所有数据。

然后,您需要执行一些正则表达式来获取随这两个请求返回的 JSON 数据。

最后,您可以开始查看有效负载并获取您需要的内容。例如,假设您在 1xBetX 列之后。所以你会期待这样的事情:

1xBet - 2.61 3.5 2.88

X 列的历史记录(此处的值为 3.5)可能如下所示:

2020-10-18 05:03:45 - 3.58
2020-10-18 04:29:41 - 3.54
2020-10-18 02:53:17 - 3.56
2020-10-17 22:25:56 - 3.58
2020-10-17 17:13:53 - 3.60
2020-10-17 13:03:37 - 3.64
2020-10-17 10:12:06 - 3.66
and so on...

所以,将所有这些放在一起,这就是我想出的:

import json
import re
import time
from datetime import datetime

import requests


headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9,pl;q=0.8",
"referer": "https://www.oddsportal.com/",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.99 Safari/537.36"
}


def get_response(url: str) -> str:
return requests.get(url, headers=headers).text


time_now_s = int(time.time())
time_now_ms = int(round(time.time() * 1000))

bookies_js = f"https://www.oddsportal.com/res/x/bookies-201014103652-{time_now_s}.js"
odds_data_js = f"https://fb.oddsportal.com/feed/match/1-1-ld9FDhEI-1-2-yje1c.dat?_={time_now_ms}"

bookies = json.loads(re.findall(r'bookmakersData=({.*});var', get_response(bookies_js))[0])
odds_data = json.loads(re.findall(r"\.dat',\s({.*})", get_response(odds_data_js))[0])

bookie = bookies['417']['WebName'] # 417 is 1xBet's code
bookies_odds = odds_data['d']['oddsdata']['back']['E-1-2-0-0-0']['odds']['417'] # current odds for a bookie
odds_sorted = dict(sorted(bookies_odds.items())).values() # sorted as on the website 1 - X - 2

print(f"{bookie} - {' '.join(str(i) for i in odds_sorted)}")


history_columns = {
"1": "4ccecxv464x0xbcsm1", # 1 column
"2": "4ccecxv464x0xbcsm2", # 2 column
"X": "4ccecxv498x0x0", # X column
}

# odds history for the X column for a given bookie
history_data = odds_data['d']['history']['back'][history_columns['X']]['417']
for item in history_data:
value, _, timestamp = item
print(f"{datetime.fromtimestamp(timestamp)} - {value}")

输出:

1xBet - 2.61 3.5 2.88
2020-10-18 05:03:45 - 3.58
2020-10-18 04:29:41 - 3.54
2020-10-18 02:53:17 - 3.56
2020-10-17 22:25:56 - 3.58
2020-10-17 17:13:53 - 3.60
2020-10-17 13:03:37 - 3.64
2020-10-17 10:12:06 - 3.66
2020-10-17 09:58:04 - 3.64
2020-10-17 08:38:52 - 3.62
2020-10-17 08:08:54 - 3.64
2020-10-17 07:44:47 - 3.62
2020-10-17 06:17:33 - 3.64
2020-10-17 06:07:36 - 3.62
2020-10-17 00:04:35 - 3.64
2020-10-16 23:54:39 - 3.62
2020-10-16 23:38:40 - 3.64
2020-10-16 18:54:48 - 3.62
2020-10-16 15:14:17 - 3.64
2020-10-16 13:27:06 - 3.66
2020-10-01 21:43:39 - 3.40

这是您在 1xBet 网页上看到的列 X 上的值。

提示:如果您遇到missing oddsdata key 问题,请查看开发人员工具并查看端点是否已更改。看这个:

enter image description here

只需获取新的端点 https://fb.oddsportal.com/feed/postmatchscore/1-ld9FDhEI-yje1c.dat?_= 或更好地完成这部分 yje1c 部分并将其与代码中的部分交换。

关于python - 使用 Python 从 oddsportal.com 抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64393605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com