【问题标题】:Webscrape Flashscore with Python/Selenium使用 Python/Selenium 进行 Webscrape Flashscore
【发布时间】:2016-06-09 22:03:27
【问题描述】:

我开始学习使用 Python 和 Selenium 抓取网站。我选择 selenium 是因为我需要浏览网站并且还必须登录。

我编写了一个脚本,它可以打开一个 firefox 窗口并打开网站 www.flashscore.com。使用此脚本,我还可以登录并导航到他们拥有的不同运动部分(主菜单)。

代码:


from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# open website
driver = webdriver.Firefox()
driver.get("http://www.flashscore.com")

# login
driver.find_element_by_id('signIn').click()

username = driver.find_element_by_id("email")
password = driver.find_element_by_id("passwd")

username.send_keys("*****")
password.send_keys("*****")

driver.find_element_by_name("login").click()

# go to the tennis section
link = driver.find_element_by_link_text('Tennis')
link.click()

#go to the live games tab in the tennis section

# ?????????????????????????????'

然后它变得更加困难。例如,我还想导航到体育领域的“现场比赛”和“完成”选项卡。这部分行不通。我尝试了很多东西,但我无法进入其中一个选项卡。在分析网站时,我发现他们使用了一些 iframe。我还找到了一些代码来切换到 iframe 窗口。但问题是,我找不到要单击的选项卡所在的 iframe 的名称。也许 iframe 不是问题,我看错了方向。 (也许问题是由某些 javascript 引起的?)

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    不,在这种情况下,iframe 不是问题。 “直播游戏”元素不在 iframe 内。通过链接文本找到它并点击:

    live_games_link = driver.find_element_by_link_text("LIVE Games")
    live_games_link.click()
    

    在实际尝试点击之前,您可能需要等待该链接可点击

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    wait = WebDriverWait(driver, 10)
    
    live_games_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "LIVE Games")))
    live_games_link.click()
    

    【讨论】:

    • 非常感谢您的反应。一个问题。运行脚本时出现错误。
    • 运行时: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver .common.by import By # open website driver = webdriver.Firefox() driver.get("flashscore.com") #转到网球部分的实时游戏选项卡 live_games_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT , "现场游戏"))) live_games_link.click()
    • 我收到错误:live_games_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "LIVE Games"))) NameError: name 'wait' is not defined
    • 我已经成功了。我只是一行:driver.implicitly_wait(10) # seconds。然后它对我有用。非常感谢你把我放在正确的路线上!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多