from selenium.webdriver import Chrome from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time with Chrome() as driver: # 打开搜狗网站 driver.get('https://www.sogou.com/') # 获取输入框 # 用标签ID "query"获取输入框 input_keys = driver.find_element(By.ID, 'query') # 输入框输入搜索内容'笔记本' input_keys.send_keys('笔记本') # 延迟两秒 time.sleep(2) # 按下回车键搜索 input_keys.send_keys(Keys.ENTER) # 延迟两秒 time.sleep(2) # 用XPath表达式获取包含推广公司的dom节点. |在XPath表达式中是或的意思 # 包含推广公司的dom节点有cite标签和类名class为"cite"的a标签 list_extension = driver.find_elements(By.XPATH, '//cite | //a[@class="cite"]') # 遍历,将查找到的公司放入列表中 list_company = [] for item in list_extension: list_company.append(item.text) print(list_company)
标签:搜狗,python,driver,list,爬取,keys,import,cite From: https://www.cnblogs.com/xmgcool/p/16936898.html