1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why does my same unchanged webscraping code keep showing different things each time I...

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    This is my code that web scrapes multiple tables with horse data from this website. "https://racing.hkjc.com/racing/information/English/raCing/RaceCard.aspx?RaceDate=2023/04/15&Racecourse=ST&RaceNo=1" What its supposed to do is click on each horse from the website, which will lead it to a popup window where it should click on the "Show All" button and print out the table shown.

    However, each time I run it, something different happens. Sometimes only half the table is printed, sometimes only one row of sometimes it works perfectly fine. Why is this happening?

    import pandas as pd
    import xlsxwriter
    from bs4 import BeautifulSoup
    from playwright.sync_api import Playwright, sync_playwright, expect
    import xlwings as xw

    wb = xw.Book("jockeyclub.xlsx")
    rc1 = wb.sheets['Race Card 1']

    #A list of the horses on the race card is already printed out on the Excel Spreadsheet

    rownum = 6
    tablerow = 20

    #the rownum variable will determine which horse from which row of the spreadsheet should be set as a variable.
    #The program will then store the horse name as a variable, find the horse name on the website and click on it.

    def scrape_ranking(url, sheet_name):
    with sync_playwright() as p:
    global rownum
    global tablerow

    horse_name = rc1.range('E' + str(rownum)).value
    print(horse_name)

    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto(url)

    with page.expect_popup() as popup_info:
    page.click('text="%s"' %horse_name)

    print("clicked on horse name")

    popup = popup_info.value
    popup.wait_for_load_state("domcontentloaded")

    popup.get_by_alt_text("Show All").click()
    print("clicked show all")

    html = popup.content()
    browser.close()

    tables = pd.read_html(html)
    df = tables[7]
    print(df)

    rc1.range('A' + str(tablerow-1)).value = horse_name

    with pd.ExcelWriter("jockeyclub.xlsx", engine="openpyxl", mode='a', if_sheet_exists='overlay') as writer:
    df.to_excel(writer, sheet_name=sheet_name, index=False, startrow=tablerow)

    tablerow = tablerow + 60 #This variable is to space out the tables
    rownum=rownum+1

    while rownum <= 13:
    url_1 = ('https://racing.hkjc.com/racing/information/English/raCing/RaceCard.aspx?RaceDate=2023/04/15&Racecourse=ST&RaceNo=1%27)
    scrape_ranking(url_1, "Race Card 1")

    Continue reading...

Compartilhe esta Página