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.baidu.com/") # 获取百度搜索框 # 用标签ID "kw"获取百度搜索框 input_key = driver.find_element(By.ID, 'kw') # 百度搜索框输入笔记本 input_key.send_keys('笔记本') # 延迟两秒后回车搜索 time.sleep(2) input_key.send_keys(Keys.ENTER) # 延迟两秒后获取广告公司的标签 # 用标签的class "ec-showurl-line"获取广告公司的标签 time.sleep(2) list_extension = driver.find_elements(By.XPATH, '//span[@class="ec-showurl-line"]') # 遍历讲打广告的公司放入列表 list_company = [] for item in list_extension: list_company.append(item.text) print(list_company)
标签:python,list,driver,爬取,搜索,标签,import,百度 From: https://www.cnblogs.com/xmgcool/p/16936880.html