#coding=utf-8
from time import sleep
import unittest
from selenium import webdriver
from selenium.webdriver import Remote
from selenium.webdriver.common.by import By#元素定位
import sys
def browser():#启动浏览器驱动
'''
driver = Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities={'platform':'ANY',
'browserName':'chrome',
'version':'',
})'''
driver = webdriver.Ie()
return driver
class MyTest(unittest.TestCase):#框架
def setUp(self):
print("开始执行用例!")
self.driver = browser()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
print('用例执行结束!')
class Page(object):#基础类
'''
页面基础类,用于所有页面的继承
'''
bbs_url = 'https://book.douban.com/'
def __init__(self,selenium_driver,base_url=bbs_url,parent=None):#初始化
self.base_url = base_url
self.driver = selenium_driver
self.timeout = 30
self.parent = parent
def on_page(self):#断言url是否相等
return self.driver.current_url == (self.base_url + self.url)
def _open(self,url):
url = self.base_url + url
self.driver.get(url)
assert self.on_page(),'获取的url和当前url不匹配.url:%s' % url
def open(self):#打开浏览器
self._open(self.url)
def find_element(self,*loc):#单个元素定位
return self.driver.find_element(*loc)
def find_elements(self,*loc):#多个元素定位
return self.driver.find_elements(*loc)
class Douban_film(Page):#页面对象封装
u'''豆瓣读书排序'''
url = '/'
#元素定位
bbs_booknum_loc = (By.XPATH,'//li/div[2]/p[1]/span[2]')#评分
bbs_bookname_loc = (By.XPATH,'//li/div[2]/h4/a')#书名
bbs_bookauther_loc = (By.XPATH,'//li/div[2]/p[2]')#作者
bbs_booktype_loc = (By.XPATH,'//li/div[2]/p[3]')#类型
bbs_bookcomments_loc = (By.XPATH,'//li/div[2]/p[5]')#评论
#元素封装
def bbs_bookname(self):#豆瓣评分
_input = self.driver.find_elements(*self.bbs_bookname_loc)# 选择页面上所有的tag name 为input 的元素
bookname = []
for i in _input:
bookname.append(i.text)#取出值并添加到数组
else:
sleep(1)
return bookname
def bbs_booknum(self):#豆瓣评分
_input = self.driver.find_elements(*self.bbs_booknum_loc)# 选择页面上所有的tag name 为input 的元素
booknum = []
for i in _input:
booknum.append(i.text)#取出值并添加到数组
else:
sleep(1)
return booknum
def bbs_bookauther(self):#作者
_input = self.driver.find_elements(*self.bbs_bookauther_loc)# 选择页面上所有的tag name 为input 的元素
bookauther = []
for i in _input:
bookauther.append(i.text)#取出值并添加到数组
else:
sleep(1)
return bookauther
def bbs_booktype(self):#类型
_input = self.driver.find_elements(*self.bbs_booktype_loc)# 选择页面上所有的tag name 为input 的元素
bookautype = []
for i in _input:
bookautype.append(i.text)#取出值并添加到数组
else:
sleep(1)
return bookautype
def bbs_bookcomments(self):#评论
_input = self.driver.find_elements(*self.bbs_bookcomments_loc)# 选择页面上所有的tag name 为input 的元素
bookaucomments = []
for i in _input:
bookaucomments.append(i.text)#取出值并添加到数组
else:
sleep(1)
return bookaucomments
def out_node(self):#排序
list1 = self.bbs_booknum()
list2 = self.bbs_bookname()
list3 = self.bbs_bookauther()
list4 = self.bbs_booktype()
list5 = self.bbs_bookcomments()
list = [[a,b,c,d,e] for a,b,c,d,e in zip(list1,list2,list3,list4,list5)]
team = list[:]
team.sort()
print('从小到大排序:%s'%team)
return team
def writetxt(self):#写入txt文件
a = self.out_node()
f1 = open('b.txt','w',encoding='utf-8')#写入文件编码格式,不写的话有时候会导致解析错误
for i in a:#循环读取内嵌列表
#if isinstance(i,list):#判断是否是一个列表
print(i)#分别打印内嵌列表里的列表
f1.writelines(i)#写入txt文件
f1.write('\n')#写入文件时换行
f1.close()
class test_Douban_film(MyTest):
u'''豆瓣读书排序'''
def test1(self):
u'''豆瓣评分按降序排列并保存到txt文件'''
p1 = Douban_film(self.driver)
p1.open()
p1.writetxt()
if __name__ == '__main__':
unittest.main()