最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python讀取html中指定元素生成excle文件示例

 更新時(shí)間:2014年04月03日 10:46:48   作者:  
這篇文章主要介紹了python讀取html中指定元素生成excle文件示例,需要的朋友可以參考下

Python2.7編寫的讀取html中指定元素,并生成excle文件

復(fù)制代碼 代碼如下:

#coding=gbk
import string
import codecs
import os,time
import xlwt
import xlrd
from bs4 import BeautifulSoup
from xlrd import open_workbook

class LogMsg:
        def __init__(self,logfile,Level=0):
                try:
                        import logging
                        #self.logger = None
                        self.logger = logging.getLogger()
                        self.hdlr = logging.FileHandler(logfile)
                        formatter = logging.Formatter("[%(asctime)s]: %(message)s","%Y%m%d %H:%M:%S")
                        self.hdlr.setFormatter(formatter)
                        self.logger.addHandler(self.hdlr)
                        #logger.setLevel()
                        if Level == 10:
                                self.logger.setLevel(logging.DEBUG)
                        elif Level == 20:
                                self.logger.setLevel(logging.INFO)
                        elif Level == 30:
                                self.logger.setLevel(logging.WARNING)
                        elif Level == 40:
                                self.logger.setLevel(logging.ERROR)
                        elif Level == 50:
                                self.logger.setLevel(logging.CRITICAL)
                        else:
                                self.logger.setLevel(logging.NOTSET)
                except:
                        print "log init error!"
                        exit(1)

        def output(self,logInfo):
                Level = self.logger.getEffectiveLevel()
                try:
                        if Level == 10:
                                self.logger.debug(logInfo)
                        elif Level == 20:
                                self.logger.info(logInfo)
                        elif Level == 30:
                                self.logger.warning(logInfo)
                        elif Level == 40:
                                self.logger.error(logInfo)
                        elif Level == 50:
                                self.logger.critical(logInfo)
                        else:
                                self.logger.info(logInfo)
                except:
                        print "log output error!"
                        exit(1)

        def close(self):
                try:
                #logging.shutdown([self.hdlr])
                        self.logger.removeHandler(self.hdlr)
                except:
                        print "log closed error!"
                        exit(1)

Logtime = time.strftime("%Y%m%d%H%M%S",time.localtime())
logFileTime = time.strftime("%Y%m%d",time.localtime())
Logfile = '/data/pyExample/logs/htmlparser_%s.log' % logFileTime
log = LogMsg(Logfile,20)


DATAPATH = '/data/pyExample/'
XLSname = 'dangjian_'+Logtime+'.xls'


if __name__ == '__main__':
   

    wbk = xlwt.Workbook(encoding = 'gbk')
    sheet = wbk.add_sheet('基本內(nèi)容導(dǎo)入模板')
    sheet.write(0,0,'內(nèi)容類型 ')
    sheet.write(0,1,'欄目名稱')
    sheet.write(0,2,'欄目編號(hào)')
    sheet.write(0,3,'內(nèi)容名稱')
    sheet.write(0,4,'時(shí)長')
    sheet.write(0,5,'關(guān)鍵字')
    sheet.write(0,6,'看點(diǎn)')
    sheet.write(0,7,'作者')
    sheet.write(0,8,'來源')
    sheet.write(0,9,'子內(nèi)容1')
    sheet.write(0,10,'子內(nèi)容2')
    xlsContent = []  
    files = os.listdir(DATAPATH)
    k = 0
    for f in files: 
        if os.path.splitext(f)[1] == '.html':
            content=[]
            log.output('當(dāng)前文件:'+f)
            htmlFile =codecs.open(DATAPATH+f,'r','gbk')
            lines = htmlFile.readlines()
            if not lines:
                log.output ('not line')
            for line in lines:
                if line.strip()=='\n':
                    log.output('該處是空行')
                else:
                    line = line.replace(' ','')
                    soup  = BeautifulSoup(line)
                    for tdd in soup.findAll('td'): 
                        #print tdd.text.encode("gbk")
                        content.append(tdd.text.encode("gbk"))      
                #print line.encode('gbk')
            htmlFile.close()   
            for i in content:
                print content.index(i),',',i
                log.output(i)
                log.output(content.index(i))
            print '----------------------------------------'
           

            folderName =  content[6]
            contentName=  content[4]      
            duration =    filter(str.isdigit, content[16])
            int_duration = string.atoi(duration)*60
            str_duration = "%i"%int_duration
            keyWord =     content[6]
            desciption =  content[36]
            videoName_1 = content[10]
            print folderName
            print contentName
            print str_duration
            print keyWord
            print desciption
            print videoName_1
            log.output('輸出xls數(shù)據(jù):'+','+folderName+',,'+contentName+','+str_duration+','+keyWord+','+desciption+',管理員,華數(shù)編輯,'+videoName_1+',,')
            print k           
            sheet.write(k+1,0,'')
            sheet.write(k+1,1,folderName)
            sheet.write(k+1,2,'')
            sheet.write(k+1,3,contentName)
            sheet.write(k+1,4,str_duration)
            sheet.write(k+1,5,keyWord)
            sheet.write(k+1,6,desciption)
            sheet.write(k+1,7,'管理員')
            sheet.write(k+1,8,'華數(shù)編輯')
            sheet.write(k+1,9,videoName_1)
            sheet.write(k+1,10,'')
            k+=1

    wbk.save(DATAPATH + XLSname)       

    print '=========================================' 

相關(guān)文章

  • 一文帶你了解Python與svg之間的操作

    一文帶你了解Python與svg之間的操作

    svgwrite是一個(gè)?Python?庫,用于生成簡單的?SVG?圖片。它提供了一組類似于繪圖的?API,使用者可以在?SVG?畫布上畫線、矩形、圓等圖形。本文主要介紹了如何利用svgwrite進(jìn)行SVG圖片的操作,需要的可以參考一下
    2023-01-01
  • 利用Python實(shí)現(xiàn)電影訂票系統(tǒng)

    利用Python實(shí)現(xiàn)電影訂票系統(tǒng)

    這篇文章主要介紹了利用Python實(shí)現(xiàn)電影訂票系統(tǒng),一部電影的詳細(xì)信息適合用?字典?結(jié)構(gòu)來存儲(chǔ),我們可以給字典里添加多個(gè)鍵值對(duì)來保存電影的名稱、座位表和宣傳時(shí)用的字符畫,需要的朋友可以參考一下
    2022-03-03
  • wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn)

    wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn)

    本文主要介紹了wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實(shí)例詳解

    Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實(shí)例詳解

    這篇文章主要介紹了Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式,以實(shí)例形式較為詳細(xì)的分析了Python函數(shù)參數(shù)的使用技巧,需要的朋友可以參考下
    2015-05-05
  • 基于Python實(shí)現(xiàn)文本文件轉(zhuǎn)Excel

    基于Python實(shí)現(xiàn)文本文件轉(zhuǎn)Excel

    Excel文件是我們常用的一種文件,在工作中使用非常頻繁。Excel中有許多強(qiáng)大工具,因此用Excel來處理文件會(huì)給我們帶來很多便捷。本文就來和大家分享一下Python實(shí)現(xiàn)文本文件轉(zhuǎn)Excel的方法,感興趣的可以了解一下
    2022-08-08
  • Python利用Django如何寫restful api接口詳解

    Python利用Django如何寫restful api接口詳解

    這篇文章主要給大家介紹了關(guān)于Python利用Django如何寫restful api接口的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • python實(shí)現(xiàn)字符串逆序輸出的幾種方法

    python實(shí)現(xiàn)字符串逆序輸出的幾種方法

    本文介紹了四種在Python中實(shí)現(xiàn)字符串逆序輸出的方法,每種方法都有其優(yōu)缺點(diǎn),下面就來介紹一下如何使用,感興趣的可以了解一下
    2024-12-12
  • Python使用logging實(shí)現(xiàn)多進(jìn)程安全的日志模塊

    Python使用logging實(shí)現(xiàn)多進(jìn)程安全的日志模塊

    這篇文章主要為大家詳細(xì)介紹了Python如何使用標(biāo)準(zhǔn)庫logging實(shí)現(xiàn)多進(jìn)程安全的日志模塊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2024-01-01
  • Pandas之to_sql()插入數(shù)據(jù)到mysql中所遇到的問題及解決

    Pandas之to_sql()插入數(shù)據(jù)到mysql中所遇到的問題及解決

    這篇文章主要介紹了Pandas之to_sql()插入數(shù)據(jù)到mysql中所遇到的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 初學(xué)Python實(shí)用技巧兩則

    初學(xué)Python實(shí)用技巧兩則

    這篇文章主要介紹了初學(xué)Python實(shí)用技巧兩則,包括可變參數(shù)的應(yīng)用級(jí)execfile函數(shù)的用法,需要的朋友可以參考下
    2014-08-08

最新評(píng)論

台州市| 农安县| 泽普县| 永年县| 台湾省| 达孜县| 普宁市| 湖南省| 锡林郭勒盟| 中山市| 东光县| 顺昌县| 彭阳县| 修文县| 林甸县| 夹江县| 邵武市| 兴安盟| 博客| 卫辉市| 尼玛县| 通河县| 武宣县| 区。| 绍兴县| 新田县| 巴塘县| 特克斯县| 嵊州市| 常德市| 吴川市| 高州市| 汕头市| 雷波县| 泸西县| 盈江县| 安康市| 沧州市| 博客| 苏尼特左旗| 阳高县|