extracting - locate the most recent 5 scores from every game - using HTML

Looking for some assistance in retrieving the most recent 5 scores, seems like I'm having trouble getting it. Can anyone lend a hand? https://i.sstatic.net/V6Lkp.png https://i.sstatic.net/ExrRe.png import selenium.webdriver as webdriver import pandas as pd from pandas import ExcelWriter from openpyxl.workbook import Workbook import time as t import xlsxwriter pd.set_option('display.max_rows', 5, 'display.max_columns', None, 'display.width', None) browser = webdriver.Firefox()

browser.get('https://www.mismarcadores.com/futbol/espana/laliga/resultados/')
print("Current Page Title: %s" %browser.title)

aux_ids = browser.find_elements_by_css_selector('.event__match.event__match--static.event__match--oneLine')

ids=[]
i = 0 
for  aux in aux_ids:
    if i < 1:
        ids.append( aux.get_attribute('id') )
        i+=1

data=[]
for idt in ids:
    id_clean = idt.split('_')[-1]   
    browser.execute_script("window.open('');")
    browser.switch_to.window(browser.window_handles[1])
    browser.get(f'https://www.mismarcadores.com/partido/{id_clean}/#h2h;overall')
    t.sleep(5)
    p_ids = browser.find_elements_by_css_selector('h2h-wrapper')
    # code to retrieve the last 5 score of each match goes here

Answer №1

I am confident that you can utilize your Firefox browser for this task, although I have not personally tested it. As a Chrome user myself, if you choose to use chromedriver, ensure to match the version with your browser and add it to your system path. The only drawback of this method is that it keeps a browser window open until the page fully loads (due to waiting for JavaScript to generate match data). Feel free to reach out if you require any further assistance. Best of luck!

https://chromedriver.chromium.org/downloads

Known issues: Occasionally, an index out of range error may occur when retrieving match data. This appears to be related to potential changes in the xpath for each link.

from selenium import webdriver
from lxml import html
from lxml.html import HtmlElement


def test():
    # URLs specified for testing purposes
    urls = ['https://www.mismarcadores.com/partido/noIPZ3Lj/#h2h;overall']
    
    # Iterating over all URLs
    for url in urls:
        print("Scores after this match {u}".format(u=url), get_last_5(url))


def get_last_5(url):
    print("processing {u}, please wait...".format(u=url))
    browser = webdriver.Chrome()
    browser.get(url)
    innerHTML = browser.execute_script("return document.body.innerHTML")
    tree: HtmlElement = html.fromstring(innerHTML)
    
    first_team = tree.xpath('//*[@id="flashscore"]/div[1]/div[1]/div[2]/div/div/a')[0].text
    
    second_team = tree.xpath('//*[@id="flashscore"]/div[1]/div[3]/div[2]/div/div/a')[0].text
   
    match_date = tree.xpath('//*[@id="utime"]')[0].text[0:8]
   
    rows = tree.xpath('//*[@id="tab-h2h-overall"]/div[1]/table/tbody')[0].getchildren()[:-1]
    
    browser.quit()
    
    match_position = None
   
    for i in range(len(rows)):
        if is_match(first_team, second_team, match_date, rows[i]):
            match_position = i + 1
            break
            
    if (match_position + 5) < len(rows):
        rows = rows[match_position:][:5]
    else:
        rows = rows[match_position:len(rows)]
    
    scores = []
    
    for row in rows:
        data = row.getchildren()[4].getchildren()[0].text_content()
       
        score = data if len(data) == 5 else data[-6:-1]
        
        scores.append(score)
    
    print("finished processing {u}.".format(u=url))
    
    return scores


def is_match(t1, t2, match_date, row):
    date = row.getchildren()[0].getchildren()[0].text
    
    team1element = row.getchildren()[2].getchildren()[0] 
   
    mt1 = team1element.getchildren()[0].text if len(team1element.getchildren()) > 0 else team1element.text
   
    team2element = row.getchildren()[3].getchildren()[0]
  
    mt2 = team2element.getchildren()[0].text if len(team2element.getchildren()) > 0 else team2element.text

    if match_date == date and t1 == mt1 and t2 == mt2:
        return True
    
    return False

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resizing a floated div causes the image to break out

My webpage has a container div with two floated left divs inside, positioned side by side. The right div contains a YouTube video and an image. However, when I resize the page, the video and picture flow out of the containing floated div instead of dropp ...

Space between flex content and border increases on hover and focus effects

My code can be found at this link: https://jsfiddle.net/walshgiarrusso/dmp4c5f3/5/ Code Snippet in HTML <body onload="resize(); resizeEnd();"> <div style="margin: 0% 13.85%; width 72.3%; border-bottom:1px solid gray;"><spanstyle="visibilit ...

Unidentified event listener

I am currently facing an issue where I keep getting the error message "addEventListerner of null" even though I have added a window.onload function. The page loads fine initially but then the error reoccurs, indicating that the eventListener is null. Str ...

Change the color of the dialog close button

I am struggling to add color to my close button in the dialog box. Here is the code for the dialog box on JSFiddle: function fnOpenNormalDialog() { // Define the Dialog and its properties. $("#dialog-confirm").dialog({ resizable: false, ...

adjusting the font size based on the size of the screen

Hi everyone, I've encountered an issue with adjusting font sizes based on screen size. My framework is Bootstrap 3 and I'm using LESS. Here is the initial part of my LESS code: @font-size-base: 16px; @font-size-large: ceil((@fo ...

Maintain the scrollable feature of the element until the final content is reached

I'm struggling to come up with the right keywords to search for my issue. I've tried using Google, but it seems my search terms are not effective. The problem I'm facing involves two relative div elements with dynamic content. This means th ...

The outer border of the Angular Material Mat Grid List is beautifully highlighted

In my project, I have a mat grid list where users can define the number of rows and columns. My goal is to display borders around each mat-grid-title cell. However, I am struggling to properly display the outermost black border for the entire mat-grid-lis ...

Ways to display notifications when the user is not actively browsing the website?

How can websites display notifications even when the user is not actively on the site? Take Facebook messenger, for instance. Even with the page closed, notifications still pop up. The same goes for Twitter, which also sends push notifications. I ...

How to access a particular tab in Bootstrap 5 using an external link

Is there a way to direct users to a specific tab upon clicking a link on another page? Check out the example below: <div class="products-btn"> <a href="products.html#pills-profile">view all</a> </div> On Pro ...

Oops! AttributeError: The function does not have the attribute 'driver'

When attempting to execute the following code snippet, an error is encountered: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from self import self class Demo: def new_login ...

How to beautifully display the hierarchy of nested SASS Maps?

In the realm of SASS programming, particularly within an Angular Theme framework, when dealing with nested maps it is possible to use @debug to log their contents. However, this method currently outputs the information on a single line. Is there a feature ...

What could be causing the value of $_POST['submit'] to be NULL in this

This $_POST['submit'] is really frustrating me. I can't seem to figure out why it's showing up as NULL. Here is the PHP code in question: if(isset($_POST['username']) && isset($_POST['email']) && i ...

Having trouble removing or adding a class to an HTML element?

I have a collection of three colored buttons. My goal is to allow users to select one button at a time, triggering it to be highlighted while the others are unselected. Each button corresponds to an object with a property called isSelected that can be set ...

What is the CSS method for altering the color of a slider's runnable track upon selection?

Seeking assistance in changing the slider track color upon selection. Struggling to achieve the desired outcome of altering the color as it slides. CSS: /* Custom Styles */ .text-size-slider { line-height: 100%; font-size: 14px; position: relative ...

How can I create a Bootstrap button with a custom href link in my code

I have created a button, is it possible to add an "href" link to this code? My link isn't working! <button class="buttoncostum"> <a href="http://example.com/"><span class="glyphicon glyphicon-th-list" style="color:white" aria-hidden=" ...

Fixing Python Selenium issue: Unable to automate clicking login on webpage

I'm at my wit's end and can't figure out what error may be occurring or if something is missing. My goal is to automate the login process using Python and Selenium. When I manually step through each line of my code, it works perfectly fine. ...

Utilizing JavaScript to assign a CSS class to an <li> list item

I am working on a page that includes a menu feature: https://jsfiddle.net/dva4zo8t/ When a menu button is clicked, the color changes and I need to retain this color even after the page is reloaded: $('[id*="button"]').click(function() { $( ...

Can the footer be adjusted to be full width only?

Is there a way to stretch only the footer on a blogger simple template to full width without altering other elements? I have looked into similar topics, but they mainly focus on WordPress or involve changing multiple elements, which is not suitable for my ...

The text is not displaying as expected due to a timeout issue

Trying to create a pop-up that functions as follows: After 3 seconds, the close button should appear, but during those 3 seconds, there will be a countdown. However, I'm encountering an issue where no text is being displayed. var n = 3; function p ...

Align elements vertically in flexbox container using Bootstrap 3 on Internet Explorer 10 and 11

Encountering a problem in IE10+11 when trying to vertically center two Bootstrap columns within a row using flexbox. The dynamic content in the columns requires the row to have a minimum height of 65px. However, if the content expands and spans multiple l ...