我可以按下一个按钮后另一个按钮使用剧作家Python网络抓取?



我正试图写一个代码,将进入这个网站"https://racing.hkjc.com/racing/information/English/racing/RaceCard.aspx?RaceDate=2023/04/06&Racecourse=HV&RaceNo=1"and点击马命名"幸运导弹"。它应该被引导到一个弹出窗口,其中包含马的所有统计数据。

然后,我想让程序点击"显示全部";按钮,因此该表不仅显示最近3个赛季的统计数据,而且显示所有赛季的统计数据。

这就是程序遇到问题的地方。它似乎找不到"显示全部"按钮。有人知道怎么解决这个问题吗?

import pandas as pd
import xlsxwriter
from bs4 import BeautifulSoup
from playwright.sync_api import Playwright, sync_playwright, expect
import xlwings as xw
def scrape_ranking(url, sheet_name):
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto(url)
with page.expect_popup() as popup_info:
page.click('text="LUCKY MISSILE"')
page.get_by_text("Show All").click()
popup = popup_info.value
popup.wait_for_load_state("domcontentloaded")

html = popup.content()
browser.close()
tables = pd.read_html(html)
df = tables[7]
with pd.ExcelWriter("hkjc.xlsx", engine="openpyxl", mode='a', if_sheet_exists='overlay') as writer:
df.to_excel(writer, sheet_name=sheet_name, index=True)

url = ('https://racing.hkjc.com/racing/information/English/racing/RaceCard.aspx?RaceDate=2023/04/06&Racecourse=HV&RaceNo=1')
scrape_ranking(url, "LUCKY MISSILE")

那个">看起来像有文本"显示全部",但是文本是栅格化到图像上的(shudder):

<img
src="/racing/content/Images/StaticFile/English/hf_allr_btn.jpg"
alt="Show All"
style="width: 92px; height: 24px"
id="hf_allr_btn_r"
class="active"
delsrc="/racing/content/Images/StaticFile/English/hf_allr_btn.jpg"
border="0"
/>

你可以用

选择这个
popup.get_by_alt_text("Show All").click()

触发一个导航,引导到一个新页面。

这个故事的寓意:使用浏览器的开发工具来检查元素,看看它到底是什么。