输入一个输入,单击它并使用Selenium检索特定信息

我是使用Python进行网页抓取的新手。我的目的是检索所关注单词的动词。例如dictionary.com有针对词的不同词性的定义,我想输入一个感兴趣的词,然后点击搜索图标,在结果页面中,我想在标题“动词”下提取信息。

hongfuxiao 回答:输入一个输入,单击它并使用Selenium检索特定信息

要提取动词标题下的信息诱导WebDriverWaitpresence_of_all_elements_located()

这是代码。

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

driver=webdriver.Chrome()
driver.get("https://www.dictionary.com/")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Accept Cookies')]"))).click()
elementsearch=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[title='Search']")))
elementsearch.send_keys("interest")
elementsearch.submit()
results=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,"//span[@class='luna-pos'][contains(.,'verb')]/following::div[1]//div[@class='default-content']//div")))
for item in results:
    print(item.text)

results1=WebDriverWait(driver,'verb')]/following::div[1]//div[@class='expandable-content']//div")))
for item in results1:
    print(item.get_attribute("textContent"))

控制台上的输出:

to engage or excite the attention or curiosity of:
Mystery stories interested him greatly.
to concern (a person,nation,etc.) in something; involve:
The fight for peace interests all nations.
to cause to take a personal concern or share; induce to participate: to interest a person in an enterprise.
to cause to be concerned; affect.
本文链接:https://www.f2er.com/3134704.html

大家都在问