python+selenium之字符串切割操作


python+selenium之字符串切割操作

  1. 在Python中自帶的一個切割方法split(),這個方法不帶參數,就默認按照空格去切割字段,如果帶參數,就按照參數去切割。
  2. 新建一個python文件,命名為base.py,里面放入打開瀏覽器等一些方法,其中包含切割的方法。代碼如下所示

#coding:utf-8

from selenium import webdriver

import time

 

class Base(object):

    driver = webdriver.Firefox()

 

    def openbrowser(self):

        self.driver.maximize_window()

        self.driver.implicitly_wait(8)

    def getbrowser(self):

        self.driver.get("https://www.baidu.com/")

        self.driver.find_element_by_id('kw').send_keys('selenium')

        self.driver.find_element_by_id('su').click()

 

    def search(self):

        re=self.driver.find_element_by_xpath("//*[@class='nums']").text

        print re

        print "============"

 

        new_str=re.split(u'約')[1]# 第一次切割得到 xxxx個,[1]代表切割右邊部分

        print new_str

        new_re=re.split(u'個')[0] # 第二次切割,得到我們想要的數字 [0]代表切割參照參數的左邊部分

        print new_re

  3.在另外一個包中新建一個測試腳本,命名為split_func.py,代碼如下所示

#coding:utf-8

from pro1.base import Base

 

b=Base()

op_b=b.openbrowser()

get_b=b.getbrowser()

 

sea=b.search()

  4.運行split_func.py腳本,測試結果如下圖所示

詳情參考:http://blog.csdn.net/u011541946/article/details/70184257

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM