iT邦幫忙

DAY 29
0

看到自己想要的結果了!沒有問題的感覺真好,至少能運作正常的簡單網站了〈複雜一點就GG了〉

程式寫到這裡,回頭看看,我真的寫的有夠菜鳥的...,看來離成功還有好遠好遠~

#! /usr/bin/env python
#-*- coding: utf-8 -*-
import urllib.request
import re
import time

class MyWebsiteSEO():

    def __init__(self, targetUrl=""):
        self.targetUrl = targetUrl
        self.urlobj = ""
        self.data = ""
        self.TagList = []
        self.TimeUse = ""
        self.siteMap = []
    
    def get_css_len(self, targetUrl, TagList ):
        targetTag=[ "link" ]
        targetAttr=[ "href" ]
        
        resultList = get_attr( TagList, targetTag, targetAttr )
        
        cssLen = 0
        for x in resultList :
            if re.search( r'^http' , x ):
                data = get_url_data( x )
                cssLen += len( str( data.decode( "utf-8" ) ) )
            elif re.search( r'^\/[\w]' , x ):
                data = get_url_data( targetUrl + x[1:] )
                cssLen += len( str( data.decode( "utf-8" ) ) )
            elif re.search( r'^\/\.\.' , x ):
                pass
        
        return cssLen

    def get_js_len(self, targetUrl, TagList ):
        targetTag=[ "script" ]
        targetAttr=[ "src" ]
        
        resultList = get_attr( TagList, targetTag, targetAttr )
        
        jsLen = 0
        for x in resultList :
            if re.search( r'^http' , x ):
                data = get_url_data( x )
                jsLen += len( str( data.decode( "utf-8" ) ) )
            elif re.search( r'^\/[\w]' , x ):
                data = get_url_data( targetUrl + x[1:] )
                jsLen += len( str( data.decode( "utf-8" ) ) )
            elif re.search( r'^\/\.\.' , x ):
                pass
        
        return jsLen

    def get_attr(self, TagList, targetTag=[ "a" ], targetAttr=[ "href" ] ):
        resultList = []
        
        for x in targetAttr:
            for y in TagList :
                if y[0] in targetTag : 
                    Attr = y[1].split(" ")
                    for z in Attr: 
                        IgCh = [ '"' , "'" ] #去除前後分號
                        if re.search( r'^'+ x +'=' , z ):
                            if z[(len(x)+1)] in IgCh: #判斷是否有分號
                                resultList.append( z[(len(x)+2):-1] )
                            else:
                                resultList.append( z[(len(x)+1):] )
        
        return resultList
        
    def get_sitemap( self ):
        # siteMap = []
        self.siteMap.append( self.targetUrl )
    
        ptr = 0
        while True:
            try :
                nextUrl = self.siteMap[ptr]
                
                if not re.search( r'^http',nextUrl ): #相對路徑
                    #應該要判斷 ../../../ 這種路徑
                    nextUrl = self.targetUrl + nextUrl
                
                data = self._get_url_data( nextUrl )
                TagList = self.get_tag( data, True )
                alinkList = self.get_link( TagList, True )
                
                #alink_to_sitMap( alinkList, self.targetUrl )
                for x in alinkList :
                    if re.search( r'^#', x ) :    #略過hash
                        continue
                    
                    rehostUrl = "^" + self.targetUrl
                    if re.search( rehostUrl, x ): #同一domin判斷
                        if not x in self.siteMap:      #避免重複
                            self.siteMap.append( x )
                            
                    if not re.search( r'^http', x ): #相對路徑
                        if not x in self.siteMap:         #避免重複
                            self.siteMap.append( x )
                
                ptr += 1
            except:
                break
        
        #self.siteMap = siteMap
    
    #just for sitemap function
    def get_link( self, TagList, flag = False ):
        targetList = [ "a" ]
        alinkList = []
        
        for y in TagList :
            if y[0] in targetList : #尋找<a>標籤
                Attr = y[1].split(" ")
                for z in Attr: 
                    IgCh = [ '"' , "'" ] #去除前後分號
                    if re.search( r'^href=' , z ):
                        if z[5] in IgCh: #判斷是否有分號
                            alinkList.append( z[6:-1] )
                        else:
                            alinkList.append( z[5:] )
                            
        return alinkList
    
    def get_tag( self, data, flag = False ):
        reStr = "<([\w]+) (.*?)>";
        reObj = re.compile( reStr.format() )
        
        targetTag = reObj.findall( str( data.decode( "utf-8" ) ) )
        
        TagList=[]
        for x in targetTag:
            TagList.append( x ) 
        
        if( flag ):
            return TagList
        else:
            self.TagList = TagList
    
    #just for sitemap function
    def _get_url_data( self, targetUrl ):
        
        urlobj = urllib.request.urlopen( targetUrl )
        data = urlobj.read()
        urlobj.close()
        
        return data

    
    def get_url_data( self, flag = False ):
        
        urlobj = urllib.request.urlopen( self.targetUrl )
        data = urlobj.read()
        urlobj.close()
        
        if( flag ):
            return data
        else:
            self.data = data
        
    def seo_analyze( self, targetUrl, level="4" ):
        self.targetUrl = targetUrl
        
        TimeStart = time.time()
        
        self.get_url_data()
        
        TimeEnd = time.time()
        TimeUse =  TimeEnd - TimeStart
        self.TimeUse = round( TimeUse, 4 )
        
        self.get_tag(self.data)
        self.get_sitemap()
        
        # return self.targetUrl
        # -- return self.urlobj
        # return self.data
        # return self.TagList
        # return self.TimeUse
        return self.siteMap

def test():
    
    #targetUrl = "http://192.168.1.106/"
    targetUrl = "https://www.python.org/"
    
    websiteobj = MyWebsiteSEO()    
    ResultData = websiteobj.seo_analyze( targetUrl ) #seo_analyze( url [,level] )
    
    print (ResultData)
    
    #ResultData has 1.表頭資料 2.回應時間 3.sitemap 4.img alt 5.css js 回應時間
    

if __name__ == "__main__":
    test()

先在這裡畫上句點吧!第二章也才有在開的理由!

以上,晚安!


上一篇
[Day 28]整理程式碼-03
下一篇
[Day 30]最後的最後
系列文
從魯蛇到蟒蛇--網頁(站)靜(動)態分析工具開發30

尚未有邦友留言

立即登入留言