使用page object模式抓取几个主要城市的pm2.5并从小到大排序后写入txt文档

时间:2023-03-08 15:28:10
使用page object模式抓取几个主要城市的pm2.5并从小到大排序后写入txt文档

#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 = webdriver.Ie()
    driver = Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                                                desired_capabilities={'platform':'ANY',
                                                                      'browserName':'firefox',
                                                                      'version':'',
                                                                     })
    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 = 'http://www.pm25.com/shanghai.html'
    
    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 PM25(Page):#页面对象封装
    u'''豆瓣读书排序'''
    url = '/'
    #元素定位
    bbs_city_loc = (By.XPATH,'//div/div[1]/h2')#城市名称
    bbs_pmindex_loc = (By.XPATH,'//div[6]/div/div[3]/div[1]/a')#PM2.5指数
    #元素封装
    def bbs_city(self):#城市名称
        cityname = []       
        pmurls = ['http://www.pm25.com/shanghai.html','http://www.pm25.com/beijing.html','http://www.pm25.com/shenzhen.html','http://www.pm25.com/guangzhou.html']
        for i in pmurls:
            self.driver.get(i)
            _input = self.driver.find_elements(*self.bbs_city_loc)# 选择页面上所有的tag name 为input 的元素
            for j in _input:
                cityname.append(j.text)#取出值并添加到数组
            else:
                sleep(1)
        return cityname
        
    
    def bbs_pm25(self):#城市指数
        citypm25 = []       
        pmurls = ['http://www.pm25.com/shanghai.html','http://www.pm25.com/beijing.html','http://www.pm25.com/shenzhen.html','http://www.pm25.com/guangzhou.html']
        for i in pmurls:
            self.driver.get(i)
            _input = self.driver.find_elements(*self.bbs_pmindex_loc)# 选择页面上所有的tag name 为input 的元素
            for j in _input:
                citypm25.append(int(j.text))#取出值并添加到数组
            else:
                sleep(1)
        return citypm25
        
    
    def out_node(self):#排序
        list1 = self.bbs_city()
        list2 = self.bbs_pm25()
        list = dict(zip(list1,list2))#(map(lambda x,y:[x,y], list1,list2))       
        print("list:%s"%list)
        dict1= sorted(list.items(),key=lambda item:item[1])#从小到大排序
        print('从小到大排序:%s'%dict1)
        return dict1
    
    def writetxt(self):#写入txt文件
        a = self.out_node()
        b = list(a)
        for c in b:
            b[b.index(c)] = list(c)#将元组内嵌列表转换为列表
        print(b)
        d = [ str(i) for i in b ]#将所有int类型转换为str类型
        print(d)
        f1 = open('a.txt','w')
        for i in d:#循环读取内嵌列表
            print(i)#分别打印内嵌列表里的列表
            f1.writelines(i)#写入txt文件
            f1.write('\n')#写入文件时换行
        f1.close()
          
class test_Douban_film(MyTest):
    u'''pm2.5排序'''
    
    def test1(self):
        u'''pm2.5排序并写入txt文件'''
        p1 = PM25(self.driver)
        p1.open()
        p1.writetxt()

if __name__ == '__main__':
    unittest.main()