在javascript网站上为python请求构建XHR链接

2024-10-04 01:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用请求删除以下网站Scorebing。 为了做到这一点,我正在浏览该网站,以找到XHR调用并获得如下url

page position 代码如下:

import requests,json

header={some data from the XHR I got using Postman}
url='https://lv.scorebing.com/ajax/score/data?mt=0&nr=1&corner=1'

response=requests.get(url=url,headers=header,data=json.dumps({}))
response.json()

没问题。我的问题是,若我切换选项卡,比如从角点切换到夹具,就不会调用新的XHR。事实上,只有“实时匹配”和“角落”允许这种直接的XHR连接。我看到加载了一些js脚本,但我不能从那里复制我的前一步

new page position

我知道我可以使用selenium来解决这个问题,可能还可以使用对页面url的常规请求和BSoup,但我不明白的是,为什么有些选项卡使用XHR调用来加载数据,而其他类似的选项卡则使用js。 我想知道如何对这些js调用进行反向工程,以获得与第一部分类似的API


Tags: 代码fromimportjsonurldata网站response
1条回答
网友
1楼 · 发布于 2024-10-04 01:27:46

首先,您应该知道Chrome中的XHR(XMLHttpRequest)将记录所有ajax请求


什么是Ajax

Ajax is a set of web development techniques using many web technologies on the client side to create asynchronous web applications.

Ajax可以通过JavaScriptjQuery实现(好吧,jQuery是一个JavaScript库,本质上是JavaScipt,但jQuery提供了一个关于ajax的API)

在示例页面中,源代码中有许多ajax请求: enter image description here

enter image description here


I would like to know how can you reverse engineer those js calls in order to get an API similar to the first part.

如果您真的只想通过源代码实现,您应该:

  1. 向页面发送GET请求
  2. 分析页面的源代码,然后迭代每个Javascript。(同时发送GET请求。)
  3. 找到所有ajax请求并发送GET请求,从中选择所需的数据

相关问题 更多 >