python - 使用 Python/Selenium 进行 Webscrape Flashscore

标签 python selenium web-scraping

我开始学习使用 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

# ?????????????????????????????'
<小时/>

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

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

最佳答案

不,在这种情况下,iframe 不是问题。 “Live games”元素不在 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()

关于python - 使用 Python/Selenium 进行 Webscrape Flashscore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37736938/

相关文章:

c# - 你如何屏幕抓取?

web-scraping - 如何在casperJs中设置输入标签的值

python - Scrapyd在具有动态ip的服务器上部署项目

python - 带有 featurewise_center 的 ImageDataGenerator() 上的 Keras fit() 验证精度不佳

python - 属性错误 : 'list' object has no attribute 'click' - Selenium Webdriver

python - Django 中的游标是否在打开的事务中运行?

javascript - 使用 Python/selenium 抓取网页内容

javascript - Protractor 测试不适用于 IE,但适用于 FF/Chrome/Safari

python - Pandas :如果符合条件,则从另一列更新列值

python - Pandas GroupBy - 仅显示具有多个独特特征值的组