如何使用Selenium Python从duckduckgo的搜索结果中提取文本

如何使用Selenium Python从duckduckgo的搜索结果中提取文本,python,selenium,xpath,webdriverwait,duckduckgo,Python,Selenium,Xpath,Webdriverwait,Duckduckgo,我试图通过以下方式获取搜索结果中duckduck的链接描述: results=browser.find_elements_by_xpath("//div[@id='links']/div/div/div[2]") description=[] for result in results: description.append(result.text) 我得到错误“list”对象没有属性“text”。我可以用类似的方法获得搜索结果标题,但是由于某些原因,我无法从这个特定的xpath提取文本。

我试图通过以下方式获取搜索结果中duckduck的链接描述:

results=browser.find_elements_by_xpath("//div[@id='links']/div/div/div[2]")
description=[]
for result in results:
  description.append(result.text)

我得到错误“list”对象没有属性“text”。我可以用类似的方法获得搜索结果标题,但是由于某些原因,我无法从这个特定的xpath提取文本。

要从
DuckDuckGo
提取搜索结果的链接描述,您必须诱导WebDriverWait,以查看所定位的所有元素,并且您可以使用以下解决方案:

  • 代码块:

    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
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://duckduckgo.com/')
    search_box = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
    search_box.send_keys("Selenium")
    search_box.submit()
    elements = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='links']/div/div/div[2]")))
    for ele in elements:
        print(ele.text)
    driver.quit()
    
  • 控制台输出:

    What is Selenium? Selenium automates browsers.That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that.
    Selenium is a mineral found in the soil. Selenium naturally appears in water and some foods. While people only need a very small amount, selenium plays a key role in the metabolism.. Why do people ...
    Selenium is a chemical element with symbol Se and atomic number 34. It is a nonmetal (more rarely considered a metalloid) with properties that are intermediate between the elements above and below in the periodic table, sulfur and tellurium, and also has similarities to arsenic.
    Selenium is a trace mineral found naturally in the soil that also appears in certain high-selenium foods, and there are even small amounts in water.. Selenium is an extremely vital mineral for the human body as it increases immunity, takes part in antioxidant activity that defends against free radical damage and inflammation, and plays a key role in maintaining a healthy metabolism.
    Introduction. Selenium is a trace element that is naturally present in many foods, added to others, and available as a dietary supplement. Selenium, which is nutritionally essential for humans, is a constituent of more than two dozen selenoproteins that play critical roles in reproduction, thyroid hormone metabolism, DNA synthesis, and protection from oxidative damage and infection [].
    Selenium is an essential trace mineral that is important for many bodily processes, including cognitive function, a healthy immune system, and fertility in both men and women.
    Your body relies on selenium, an important mineral, for many of its basic functions, from reproduction to fighting infection. The amount of selenium in different foods depends on the amount of ...
    Overview Information Selenium is a mineral. It is taken into the body in water and foods. People use it for medicine. Most of the selenium in the body comes from the diet. The amount of selenium ...
    Selenium WebDriver. The biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on a remote machine using the Selenium Server it marks a leap forward in terms of browser automation.
    Downloads. Below is where you can find the latest releases of all the Selenium components. You can also find a list of previous releases, source code, and additional information for Maven users (Maven is a popular Java build tool).
    

    • 您不必为空列表创建for循环。。。尝试使用以下代码:

      results=driver.find_elements_by_xpath("//div[@id='links']/div/div/div[2]")
      
      description=[]
      
      for result in results:
          description.append(result.text)
      
      例如: 为了测试这一点,我只需在DuckDuckGo中输入'hmm',这样URL就

      输出:

      嗯,开发“新高斯2020”。。。嗯,持有PSA现代釜山N。。。嗯,新的超大型油轮命名为“环球”。。。2019年新年贺词;HMM的未来计划;嗯,202年的蓝图。。。嗯,签了正式合同

      Hmm定义(通常用于表示深思熟虑的专注、犹豫、怀疑或困惑)。请参阅更多

      二,� 过去强调一个人已经问了一个问题,正在等待答案,但现在告诉圣诞老人真相,对一个小男孩或小女孩来说,什么是最重要的部分?盒子

      搜索结果如下:


      当您尝试将desctiption[]写入控制台时会发生什么?您可以发布完整的错误消息和您尝试读取的部分HTML吗?它们列出了显示为url的项目示例,传递给浏览器。get(url):@michael请不要更改您已收到充分研究答案的问题。一旦你收到规范的答案,改变问题会使所有现有的答案无效,对未来的读者可能没有用处。如果您的要求发生了变化,请随时提出新问题。StackOverflow贡献者将很乐意帮助您。目前,我已将问题还原为初始状态。注意:问题已编辑。。。它以前有一个空列表的for循环,我删除了这个循环,这是不必要的。但是,在获取描述时,我仍然遇到同样的问题,无法获得期望的结果。我用包含的新代码更新了问题
      from selenium import webdriver
      
      driver=webdriver.Chrome()
      driver.get('https://duckduckgo.com/?q=hmm&t=h_&ia=web')
      
      results=driver.find_elements_by_xpath("//div[@id='links']/div/div/div[2]")
      
      description=[]
      
      for result in results:
          description.append(result.text)
      
      print(description[0])
      print(' ')
      print(description[1])
      print(' ')
      print(description[2])