从下拉菜单中选择Python选项

2024-06-01 09:08:35 发布

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

我找了很多方法来解决这个问题,但没有找到任何我能解决的方法。 基本上我有这个网页:

http://databank.worldbank.org/data/embed-int/Table-1-SDDS-new/id/4f2f0c86

我要做的是用Python改变国家,以便从HTML中提取数据(我已经知道如何提取数据)。 关键是,我不知道如何改变这个国家。你能帮我吗?你知道吗

我看到了许多类似的解决方案,但可能是由于我缺乏使用HTML的经验,我没有很好地理解它们。你知道吗

先谢谢你。你知道吗


Tags: 数据方法orghttp网页newdatahtml
2条回答

你最好使用硒这种方法。在这种情况下,您可以单击下拉列表并使用输入框发送您感兴趣的国家,然后输入。你知道吗

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

d = webdriver.Chrome()
d.get("http://databank.worldbank.org/data/embed-int/Table-1-SDDS-new/id/4f2f0c86")
dropdown = WebDriverWait(d,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".chosen-single")))
dropdown.click()
input = d.find_element_by_css_selector('.chosen-search input')
input.send_keys('Brazil')
input.send_keys(Keys.RETURN)

我也不知道如何从下拉菜单中选择选项,但这里是worldbank api

相关问题 更多 >