【问题标题】:Python selenium scrape - particular informations from block of codePython selenium scrape - 来自代码块的特定信息
【发布时间】:2021-11-18 23:12:21
【问题描述】:

我需要从本地投注site 中抓取(并收集)一些实时体育统计数据。我为此使用python。那么,如何添加到 python 数据框的实际状态?在我的代码中,我有一个变量“games_container”,但我不知道如何提取特定信息(运动、联赛、A 队、B 队、结果......)。感谢您的帮助。

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import time
import requests
from time import gmtime, strftime, localtime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import re
from bs4 import BeautifulSoup
import pandas as pd
import numbers
import matplotlib

from selenium.webdriver import ActionChains

DRIVER_PATH = 'C:\\executables\\chromedriver.exe'

options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")

driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)

driver.get("https://www.nike.sk/live/prehlad")

time.sleep(20)


games_container = soup.find_all(class_='sport-content-div sport-content clearfix')

预期输出(熊猫数据框的形状):

.................................. 编辑: 从这部分代码中提取“home short label 1”、“game result”和“home short label 2”是否可以进行循环?

................................. EDIT_2: 当我使用时:

driver.get("https://www.nike.sk/live/prehlad")
time.sleep(15)

soup = BeautifulSoup(driver.page_source,'html5lib')
games = soup.find_all('div',{'data-atid':'live-sport-box'})

for game in games:
    print(game.find('h2').getText())
    labels = game.find('h4',{'class':'game-content-title'}).findChildren('span',recursive=False)
    label1 = labels[0].getText()
    label2 = labels[2].getText()
    result = labels[1].getText()
    print(f"{label1} : {label2} : {result}")

在我的输出中,每项运动只有 1 场比赛(1 行):

Tenis  (21)Podľa turnaja/súťažePodľa času
 Cristian J.A. : Krunic A.  : 0 - 0
Futbal  (4)Podľa turnaja/súťažePodľa času
 Shaanxi Changan : Chengdu Rongcheng  : 0 - 0
Hokej  (4)Podľa turnaja/súťažePodľa času
 MHC Torpedo : MHC Arlan  : 4 - 1

如何打印(迭代)所有运动的所有比赛?

【问题讨论】:

  • 你能分享数据的例子吗?您指的是哪些数据,比如说第一条记录?我知道你写了sport, league, team A, team B, result...,但让我知道首先记录一下这些值是多少?
  • 谢谢。我用预期的输出更新了我的答案
  • 该网站也有其他运动的数据,因此在这些情况下目标 A 和目标 B 将不存在。这很棘手。
  • 是的,有很多运动,所以我需要在输出列“运动”。我喜欢刮所有运动
  • 我可以使用得分 A 和得分 B,而不是目标...

标签: python-3.x selenium web-scraping screen-scraping


【解决方案1】:

试试这样的:

driver.get("https://www.nike.sk/live/prehlad")
time.sleep(15)

soup = BeautifulSoup(driver.page_source,'html5lib')
games = soup.find_all('div',{'data-atid':'live-sport-box'})
for game in games:
    print(game.find('h2').getText()) # Prints the heading of the sports
    print(game.find('ul').getText()) # Prints all the details of that particular sport

要将特定数据分别提取到像团队和结果这样的变量中,请使用RegexSupporting link

这些详细信息位于h4 标签内的span 中。我们也可以使用findChildren()。 要获得所有团队及其结果,请尝试如下:

for game in games:
    print(game.find('h2').getText())
    labels = game.find('h4',{'class':'game-content-title'}).findChildren('span',recursive=False)
    label1 = labels[0].getText()
    label2 = labels[2].getText()
    result = labels[1].getText()
    print(f"{label1} : {label2} : {result}")

    # Or Iterate over labels:
    # for label in labels:
    #     print(label.getText())
Tenis  (4)Podľa turnaja/súťažePodľa času
 Caruso S.
0 - 0
Fritz T. 
Futbal  (1)Podľa turnaja/súťažePodľa času
 CD Guadalajara ž.
2 - 0
CF Pachuca ž. 

【讨论】:

  • 谢谢。我现在就去试试。
  • 请问,你能看看我编辑的问题吗?
  • @314mip - 我已经更新了相同的答案。更新问题时还要添加您尝试过的内容。
  • @314mip - 我也试过了。它不会为我抛出任何错误。已更新示例输出。
  • @314mip - 好的 - 你试图解决什么问题?在问题中更新相同的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多