使用python硒键入文本后,如何从搜索建议中获取值? [英] How to get values from search suggestions after keying in text using python selenium?

查看:55
本文介绍了使用python硒键入文本后,如何从搜索建议中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当您在搜索栏中的

我试图让它返回该下拉框中的值的列表,字典或数据框.

例如

  {'AAPL':['Apple Inc.','Equity-NMS','https://finance.yahoo.com/quote/AAPL?p = AAPL& .tsrc = fin-srch'],'AAPL.BA':['Apple Inc.','Equity-BUE','https://finance.yahoo.com/quote/AAPL.BA?p = AAPL.BA& .tsrc = fin-srch'],.....} 

  ['AAPL','Apple Inc.','Equity-NMS','https://finance.yahoo.com/quote/AAPL?p = AAPL& .tsrc = fin-srch']['APPL.BA','Apple Inc.','Equity-BUE','https://finance.yahoo.com/quote/AAPL.BA?p = AAPL.BA& .tsrc = fin-srch']] 

最后一个值是单击链接后的超链接.

到目前为止,这是我的代码,

  options = Options()驱动程序= webdriver.Chrome(executable_path = r'C:\ Program Files \ chromedriver \ chromedriver.exe',options = options)url ="https://finance.yahoo.com/"driver.get(网址)time.sleep(2)inputElement = driver.find_element_by_xpath('//* [@ id ="yfin-usr-qry"]')inputElement.send_keys('apple')time.sleep(2)web_elem_list = driver.find_elements_by_xpath(".//ul [@ class ='M(0)']/li/div/div")建议= [web_elem_list中web_elem的web_elem.text]打印(建议)driver.close() 

但是输出始终为空,我似乎无法在建议框中找到元素.

我还尝试使用 web_elem_list = driver.find_elements_by_xpath(".//ul [@ class ='f470fc71']/li/div/div")

但是它没有任何值.

我该怎么办

  1. 找到建议框的xpath吗?
  2. 创建所有结果(包括超链接)的数据框,字典或列表.

更新:

我已经弄清楚了问题的第一部分,xpath的/div太多了.我更新了问题,现在部分代码可以正常工作.

但是我仍然没有弄清楚问题的第二部分,我仍然无法获得"Equity-NMS"部分和超链接.

解决方案

对脚本的等待和xpath进行了一些更改.结果将是熊猫数据框中的建议数据.

从硒导入Webdriver的

 从selenium.webdriver.support导入EC的预期条件来自selenium.webdriver.common.by导入方式从selenium.webdriver.support.ui导入WebDriverWait导入时间将熊猫作为pd导入options = webdriver.ChromeOptions()options.add_argument('开始最大化')驱动程序= webdriver.Chrome(选项=选项)url ="https://finance.yahoo.com/"driver.get(网址)WebDriverWait(驱动程序,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="yfin-usr-qry"]')))).send_keys('apple')WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//div//* [包含(text(),'Symbols')]")))))web_elem_list = driver.find_elements_by_xpath(".//div [@ data-test ='search-assist-input-sugglst']/div/ul [1]/li/div")结果= pd.DataFrame()对于web_elem_list中的web_elem:建议= []code = web_elem.find_element_by_xpath("./div/div").texthints.append(代码)name = web_elem.find_element_by_xpath("./div/div/following-sibling :: div").texthints.append(name)Equity = web_elem.find_element_by_xpath("./div/following-sibling :: div").textproposal.append(equity)超链接= f'https://finance.yahoo.com/quote/{code}?p = {code}& .tsrc = fin-srch'proposal.append(超链接)结果= results.append(pd.Series(建议),ignore_index = True)打印(结果)driver.close() 

输出:

When you enter something for example apple into the search bar at https://finance.yahoo.com/ there is a search suggestions menu that appears.

I am trying to get it to return a list, dictionary or dataframe of the values in that drop down box.

For example

{'AAPL':['Apple Inc.','Equity - NMS','https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch'],
 'AAPL.BA':['Apple Inc.','Equity - BUE','https://finance.yahoo.com/quote/AAPL.BA?p=AAPL.BA&.tsrc=fin-srch'],
  .....}

or

['AAPL','Apple Inc.','Equity - NMS','https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch']
['APPL.BA','Apple Inc.','Equity - BUE','https://finance.yahoo.com/quote/AAPL.BA?p=AAPL.BA&.tsrc=fin-srch']

The last value is the hyperlink from clicking the link.

Here is my code so far,

options = Options()

driver = webdriver.Chrome(executable_path=r'C:\Program Files\chromedriver\chromedriver.exe',options=options)

url = "https://finance.yahoo.com/"
driver.get(url)
time.sleep(2)

inputElement = driver.find_element_by_xpath('//*[@id="yfin-usr-qry"]')
inputElement.send_keys('apple')
time.sleep(2)

web_elem_list = driver.find_elements_by_xpath(".//ul[@class='M(0)']/li/div/div")
suggests = [web_elem.text for web_elem in web_elem_list]

print(suggests)

driver.close()

But the output keeps coming up empty, I cant seem to locate the elements in the suggestion box.

I also tried using web_elem_list = driver.find_elements_by_xpath(".//ul[@class='f470fc71']/li/div/div")

But it doesnt have any values.

How do I,

  1. Find the xpath of the suggestion box?
  2. Create a dataframe, dictionary or list of all the results (including the hyperlink).

UPDATE:

I have figured out the first part of the question, the xpath had one too many /div. I updated my question and the part of the code works now.

But I still haven't figured out the second part of the question, I still cant get the "Equity - NMS" part and hyperlinks.

解决方案

Made some changes to your script around waiting and xpaths. The result will be the suggested data in a pandas dataframe.

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

options=webdriver.ChromeOptions()
options.add_argument('start-maximized')

driver = webdriver.Chrome(options=options)
url = "https://finance.yahoo.com/"
driver.get(url)

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yfin-usr-qry"]'))).send_keys('apple')

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div//*[contains(text(),'Symbols')]")))
web_elem_list = driver.find_elements_by_xpath(".//div[@data-test='search-assist-input-sugglst']/div/ul[1]/li/div")
results = pd.DataFrame()

for web_elem in web_elem_list:
    suggests=[]

    code=web_elem.find_element_by_xpath("./div/div").text
    suggests.append(code)

    name=web_elem.find_element_by_xpath("./div/div/following-sibling::div").text
    suggests.append(name)

    equity=web_elem.find_element_by_xpath("./div/following-sibling::div").text
    suggests.append(equity)

    hyperlink=f'https://finance.yahoo.com/quote/{code}?p={code}&.tsrc=fin-srch'
    suggests.append(hyperlink)

    results=results.append(pd.Series(suggests), ignore_index=True)

print(results)

driver.close()

Output:

这篇关于使用python硒键入文本后,如何从搜索建议中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆