如何用excel urldecode解码把url编码转为汉字?

最近有个项目需要批量将网站链接urldecode解码。也可以调用站长工具进行解码但是需要多个步骤因此再网上找了个vba脚本很方便实用。

zo994m3ydl

以上是cnzz的截图,只能显示最近7天的数据,可以每周下载一次,最好能每天都看,seo是一个持续的过程。

说了那么多,进入正题。把访问明细表导出来后会发现有些页面是其他搜索页,比如博客园自带的搜索

tqji3ce1bl

这些搜索页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

 

相关推荐

二维表转一维表

什么是一维表 在 Excel 中常见的是二维表 年度 北京 天津 上海 杭州 2015 200 100 300 ...

power query 使用常用正则

经常会使用power query 清洗数据,但是经常会存在使用M函数又太复杂的情况,因此需要借助正则表达式。进 ...