|
楼主 |
发表于 2017-10-23 12:41
|
显示全部楼层
本帖最后由 苏子龙 于 2017-10-23 12:48 编辑
- Sub Main()
- Dim strText As String, Url As String, KdNum As String
- Dim i%, Mh As Object, Arr(1 To 99, 1 To 2)
- Range("q2:r100").ClearContents: [p2:p3] = "" '前期处理
-
- KdNum = [o2]
- Url = "http://m.kuaidihelp.com/express/queryResult"
- With CreateObject("MSXML2.XMLHTTP") 'CreateObject("WinHttp.WinHttpRequest.5.1")'
- .Open "POST", Url, False 'POST,GET 根据需要更改
- .setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
- .setRequestHeader "X-Requested-With", "XMLHttpRequest" '这个一定要加
- ' .setRequestHeader "Referer", "http://m.kuaidihelp.com/express/queryResult?word=" & KdNum '这个可以省略
- .Send "brand=&waybill=" & KdNum
- ' strText = .responsetext
- ' Debug.Print strText '通过立即窗口查看提取出来的数据
- ' Debug.Print TestRegUni(strText)
- ' [a29] = strText
- ' [a43] = TestRegUni(strText)
- strText = TestRegUni(.responsetext) '转译成正常的文本
- strText = Replace(strText, "<br\/>", " ") '把时间和日期中间的<br\/>替换成空格
- End With
- '######################对数据进行处理并写入到单元格###########################
- With CreateObject("vbscript.regexp") '用正则提取数据
- .Global = True
- .Pattern = "date"":""(.+?)"",""info"":""(.+?)""}"
- Set Mh = .Execute(strText)
- If Mh.Count = 0 Then MsgBox "该快递单号无法识别": Exit Sub
- For i = 1 To Mh.Count
- Arr(i, 1) = Mh(i - 1).submatches(0)
- Arr(i, 2) = Mh(i - 1).submatches(1)
- Next
- End With
-
- [q2].Resize(99, 2) = Arr
- [p2] = Split(Split(strText, """,""brand_key")(0), "name"":""")(1)
- [p3] = Split(Split(strText, """,""data")(0), "msg"":""")(1)
- Range("Q2:Q100").NumberFormat = "yyyy-mm-dd hh:mm"
- End Sub
- Function TestRegUni(str As String) As String '用正则提取后处理转译,把response的内容转成常规内容
- Dim strTemp$, i%, y%, Arr(1 To 10000, 1 To 2), 定义足够的的数组
- Dim iReg As Object, iMch As Object, Mch As Object
- Dim d As Object
- strTemp = str
- Set d = CreateObject("scripting.dictionary") '字典去重复内容,可能提高运行速度!
- Set iReg = CreateObject("vbscript.regexp") '提取转译内容
- iReg.Global = True
- iReg.Pattern = "\\u\w{4}"
- Set iMch = iReg.Execute(str)
- For Each Mch In iMch
- If Not d.exists(Mch) Then
- d(Mch) = ""
- y = y + 1
- Arr(y, 1) = ChrW(CLng(Replace(Mch.Value, "\u", "&h")))
- Arr(y, 2) = Mch.Value
- End If
- Next
- For i = 1 To d.Count
- strTemp = Replace(strTemp, Arr(i, 2), Arr(i, 1))
- Next
- TestRegUni = strTemp
- Set iReg = Nothing
- Set d = Nothing
- End Function
复制代码
继续学习中。。。 |
|