#--coding=utf8-- from selenium import webdriver import time import logging import os.path ''' 搜索取搜索的条数 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.log文件存储在项目根目录下Logs文件夹下 3. 这个日志类,支持INFO,ERROR两种日志级别 4. 日志里,每行日志输出,如上图,时间日期+执行类名称+日志级别+日志描述 ''' class Test(object): #日志格式 uplu = os.path.abspath(os.path.dirname(os.getcwd())) logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a,%d %b %Y %H:%M:%S', filename=uplu + '/logs/test.log', filemode='w' ) def __init__(self): self.driver = webdriver.Firefox(executable_path='D:seleniumpythondrivergeckodriver.exe') self.driver.get('http://www.baidu.com') self.driver.maximize_window() self.driver.implicitly_wait(8) logging.info('启动打开页面成功!') def get_num(self): self.driver.find_element_by_id('kw').send_keys('selenium') self.driver.find_element_by_id('su').click() element = self.driver.find_element_by_class_name('nums') logging.info('获取文本成功!') return element.text def split_word(self): text = self.get_num() new_string = text.split('约')[1] num = new_string.split('个')[0] logging.debug('获取数据成功!') return num def exit(self): self.driver.quit() test = Test() test.get_num() nums = test.split_word() print(nums) time.sleep(6) test.exit()