如何用vba获取QQ网络数据采集时的bkn参数?

在进行QQ相关内容的网络数据采集时,经常看到需要附带bkn参数的时候。

https://qun.qq.com/cgi-bin/qunwelcome/myinfo?callback=?&bkn=114713469

经过分析QQ的网页JS,找到了bkn参数的获取算法如下:

e.getCSRFToken = function() {
		var t = e.cookie("skey");
		if (t) {
			for (var r = 5381, n = 0, o = t.length; o > n; ++n) r += (r << 5) + t.charAt(n).charCodeAt();
			return this.CSRFToken = 2147483647 & r
		}

其中skey参数为登录QQ后的Cookie中附带的参数。

根据上述的Javascript算法,可以将其转换为如下的vba代码:

Function GetBkn(ByVal sKey As String)
    Dim sJS As String
    sJS = " var t = '" & sKey & "';for (var r = 5381, n = 0, o = t.length; o > n; ++n) r += (r << 5) + t.charAt(n).charCodeAt();var bkn= 2147483647 & r "
    Dim oHtml As Object
    '定义HtmlDocument对象
    Set oHtml = CreateObject("htmlfile")
    Dim oWindow As Object
    Set oWindow = oHtml.parentWindow
    With oWindow
        .execScript sJS
      GetBkn = .bkn
    End With
    Set oWindow = Nothing
    Set oHtml = Nothing
End Function
Sub QQ1722187970()
    Dim sKey As String
    sKey = "@GRqYzhxza"
    Debug.Print GetBkn(sKey)
End Sub
       

发表评论