元素不响应Python请求

2024-09-21 00:18:16 发布

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

我想从这个页面https://www.betexplorer.com/soccer/estonia/esiliiga/elva-flora-tallinn/Q9KlbwaJ/中获取存档中的最后一个赔率,但我无法通过请求获得它。如果不与Selenium交互,我如何获得它? 要在开发者工具中触发归档几率页面,我需要将鼠标悬停在奇数上。 enter image description here

enter image description here

代码

 url = "https://www.betexplorer.com/archive-odds/4l4ubxv464x0xc78lr/14/"
 headers = {
            "Referer": "https://www.betexplorer.com",
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
               }
Json = requests.get(url, headers=headers).json()


Tags: httpscomurlwww页面headersflorasoccer
1条回答
网友
1楼 · 发布于 2024-09-21 00:18:16

由于站点是由JavaScript加载的,requests不起作用。我使用selenium加载页面,在加载所有内容后提取完整的源代码

然后使用beautifulsoup创建soup对象以获取所需数据

从源代码中可以看到<tr>data-bid是用来获取odds数据的

我提取了所有的data-bid,并将它们一个一个地传递到您在问题末尾提供的URL

此代码将以JSON格式获取所有赔率数据

import time
from bs4 import BeautifulSoup
import requests
from selenium import webdriver

base_url = 'https://www.betexplorer.com/soccer/estonia/esiliiga/elva-flora-tallinn/Q9KlbwaJ/'
driver = webdriver.Chrome()
driver.get(base_url)

time.sleep(5)

soup = BeautifulSoup(driver.page_source, 'html.parser')
t = soup.find('table', attrs= {'id': 'sortable-1'})
trs = t.find('tbody').findAll('tr')

for i in trs:
    data_bid = i['data-bid']
    url = f"https://www.betexplorer.com/archive-odds/4l4ubxv464x0xc78lr/{data_bid}/"
    headers = {"Referer": "https://www.betexplorer.com",'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}
    Json = requests.get(url, headers=headers).json()
    
    # Do what you wish to do withe JSON data here....

相关问题 更多 >

    热门问题