Überspringen Sie Zeilen in eine für die Schleife, wenn eine Variable mit Python [geschlossen] leer ist, leer istPython

Python-Programme
Anonymous
 Überspringen Sie Zeilen in eine für die Schleife, wenn eine Variable mit Python [geschlossen] leer ist, leer ist

Post by Anonymous »

Ich kratze Fußballspiele Ergebnisse und Quotendaten von einer Website, aber wenn ein Fußballspiel keine Chancen hat, möchte ich diese Reihe gerne überspringen. Im Moment, und ich weiß nicht warum, nimmt mein Code stattdessen die Chancen des nächsten Spiels ein. Und das will ich nicht, aber ich möchte nur jedes Spiel ohne Gewinnchancen überspringen < /p>
Dies ist mein Code: < /p>

Code: Select all

from typing import Generator
from requests_html import HTMLSession
import pandas as pd
import numpy as np
from itertools import islice
import mysql.connector
from sqlalchemy import create_engine
import os
import winsound
import pygame
from pygame import mixer # Load the required library
from openpyxl import load_workbook
from openpyxl.styles import PatternFill

matchlink =  "https://www.betexplorer.com/football/algeria/ligue-2/results/"

def _get_rows(url: str) -> Generator[dict[str, str], None, None]:
session = HTMLSession()

r = session.get(matchlink)

allmatch = r.html.find(".in-match")
results = r.html.find(".h-text-center a")
matchodds = r.html.find("[data-odd]")

odds = [matchodd.attrs["data-odd"] for matchodd in matchodds]

idx = 0
for match, res in zip(allmatch, results):

print(f"{match.text} Z {res.text} {', '.join(odds[idx:idx+3])}")
yield {
"match": match.text,
"result": res.text,
"odds": ", ".join(odds[idx : idx + 3]),
"best_bets": (odds[idx]), "oddtwo": (odds[idx+1]), "oddthree": (odds[idx+2]),
}

idx += 3

if __name__ == "__main__":
df = pd.DataFrame(_get_rows(matchlink))#.set_index("match")
df[['home','away']] = df['match'].str.split("-",expand=True)
df[['scorehome','scoreaway']] = df['result'].str.split(":",expand=True)
df = df.astype({'scorehome':'int'})
df = df.astype({'scoreaway':'int'})
cols = ['scorehome', 'scoreaway']
df['tournament'] = "Alg-B"
df['result'] = df['result'].str.replace(':','-')
colss = ['home', 'away', 'result']
df = df[['home','away','scorehome','scoreaway','result', 'tournament']]

print(df)
Ich hoffe, meine Frage ist klar. Tut mir leid, ob es nicht ist, aber ich bin ein Neuling von Python

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post