如何用excel urldecode解碼把url編碼轉爲漢字?

統計分析可以反映出網站運營的情況,並根據實際作出相應的調整,是站長必需的基礎技能。ytkah感覺最好用的是谷歌統計,裏面有個搜索關鍵詞及對應受訪頁面,這個功能對優化用處很大,但大家都知道訪問不太順暢。statcounter也有相似的功能,但免費版只能顯示最近幾天或最近幾百條的記錄。bd統計、cnzz也有類似的功能,只是不太明顯,沒注意的話可能發現不到,沒錯,就是訪問明細那。最相關的文章:如何從統計中批量獲取BD搜索關鍵詞及對應的入口頁面?

搜索關鍵詞及對應受訪頁面

  以上是cnzz的截圖,只能顯示最近7天的數據,可以每週下載一次,最好能每天都看,seo是一個持續的過程。

  說了那麼多,進入正題。把訪問明細表導出來後會發現有些頁面是其他搜索頁,比如博客園自帶的搜索

  這些搜索頁url是經過編碼的,如何用excel urldecode解碼把url編碼轉爲漢字?如上圖所示,A1轉爲A5的形式

  在excel左上角的菜單,點擊 “開發工具” - 選“Visual Basic”,在新界面中選 “插入” - “模塊”,輸入如下代碼

Function URLDecode(ByVal strIn)
        URLDecode = ""
        Dim sl: sl = 1
        Dim tl: tl = 1
        Dim key: key = "%"
        Dim kl: kl = Len(key)
        sl = InStr(sl, strIn, key, 1)
        Do While sl > 0
            If (tl = 1 And sl <> 1) Or tl < sl Then
                URLDecode = URLDecode & Mid(strIn, tl, sl - tl)
            End If
            Dim hh, hi, hl
            Dim a
            Select Case UCase(Mid(strIn, sl + kl, 1))
                Case "U" 'Unicode URLEncode
                    a = Mid(strIn, sl + kl + 1, 4)
                    URLDecode = URLDecode & ChrW("&H" & a)
                    sl = sl + 6
                Case "E" 'UTF-8 URLEncode
                    hh = Mid(strIn, sl + kl, 2)
                    a = Int("&H" & hh) 'ascii碼
                    If Abs(a) < 128 Then
                        sl = sl + 3
                        URLDecode = URLDecode & Chr(a)
                    Else
                        hi = Mid(strIn, sl + 3 + kl, 2)
                        hl = Mid(strIn, sl + 6 + kl, 2)
                        a = ("&H" & hh And &HF) * 2 ^ 12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
                        If a < 0 Then a = a + 65536
                        URLDecode = URLDecode & ChrW(a)
                        sl = sl + 9
                    End If
                Case Else 'Asc URLEncode
                    hh = Mid(strIn, sl + kl, 2) '高位
                    a = Int("&H" & hh) 'ascii碼
                    If Abs(a) < 128 Then
                        sl = sl + 3
                    Else
                        hi = Mid(strIn, sl + 3 + kl, 2) '低位
                        a = Int("&H" & hh & hi) '非ascii碼
                        sl = sl + 6
                    End If
                    URLDecode = URLDecode & Chr(a)
            End Select
            tl = sl
            sl = InStr(sl, strIn, key, 1)
        Loop
        URLDecode = URLDecode & Mid(strIn, tl)
    End Function

  保存

  關掉VB窗口,直接在A5單元格輸入框輸入函數=URLDecode(A1),就可以得到所要的結果了

  如果要把中文編譯成編碼呢?也是可以的

 

Function GetURL$(txt$, Optional LC = &H804)     
    Dim a() As Byte: a = StrConv(txt, vbFromUnicode, LC)
    For i = 0 To UBound(a)
        GetURL = GetURL & "%" & Right("0" & Hex(a(i)), 2)
    Next
End Function

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章