使用Selenium获取天气数据

2024-05-14 21:10:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我从this weather.gov page一整天都在努力获取过去10年的平均气温。我希望我的python脚本执行以下操作:

a)关岛国际机场 b) 来自产品的日历日摘要 c) 2011-2021年(从年份范围) d) 来自变量的平均温度 e) 离开视野

查看之后,我最终将使用beautiful soup打印出表格,但我需要先完成这一步

到目前为止,我掌握的情况如下:

from selenium import webdriver
import time

driver=webdriver.Chrome(executable_path=r"C:\myPath\chromedriver.exe")
url = "https://w2.weather.gov/climate/xmacis.php?wfo=guam"
driver.get(url)
time.sleep(2)

driver.find_element_by_xpath('//*[@class="input_block"]/select/option[2]').click()
time.sleep(2)

然而,我得到了以下错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class="input_block"]/select/option[2]"}
  (Session info: chrome=88.0.4324.104)

我们非常感谢为解决这个问题提供的任何帮助


Tags: importurlinputtimedriverseleniumsleepelement
2条回答

当您有一个iframe时,您需要切换上下文以便在其中工作,您的iframe名称是'routine'-usedriver.switch_to.frame("name")driver.switch_to.frame("id of the element")来处理它

url = "https://w2.weather.gov/climate/xmacis.php?wfo=guam"
driver.get(url)
time.sleep(2)
driver.switch_to.frame('routine')
driver.find_element_by_xpath('//*[@class="input_block"]/select/option[2]').click()
time.sleep(2)

相反,您可以使用服务https://gridforecast.com/api当前天气气温统计

相关问题 更多 >

    热门问题