ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 396|回复: 3

物流抓取神器制作请大神解决

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-3-30 17:37 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Private Sub 批量获取1()
On Error GoTo aa
Dim url2$, htmlcode2$
Dim rowa2!, rowb!, i!, j!, ms!

Set OH = CreateObject("Msxml2.ServerXMLHTTP")
dengluming = Sheet2.Cells(1, 2)
denglumima = Sheet2.Cells(2, 2)
fp = Sheet2.Cells(3, 2)


'登录-----------------------------------------------------------
OH.Open "post", "http://ssa.jd.com/sso/login", False
OH.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Data = "fp=" & fp & "&username=" & dengluming & "&password=" & denglumima & ""
OH.send (Data)



rowa2 = [A65536].End(3).Row
v = "共计查询" & rowa2 - 2
v1 = v & "单,预计耗时"
v3 = v1 & Round(rowa2 / 2 / 60, 0) & "分钟,温馨提示:查询期间EXECL会失去响应,请提前保存好数据。"
'Select Case MsgBox(v2, 52, "警告")
'Case 6
   
   For i = 3 To rowa2
    t = Timer
    Do While Timer - 0.0000001 < t '防止服务器堵塞
       Debug.Print
    Loop
   
    If Cells(i, 1) = "" Then GoTo aa
   
    OH.Open "post", "http://waybill.jd.com/waybill/trackInfoByCode", False
    OH.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    Data = "code=" & Cells(i, 1)
    OH.send (Data)
    htmlcode2 = OH.responsetext



    'htmlcode2 = getHTML(Cells(i, 1)) '得到点击查询后返回的源代码
    Dim s12 As String, s2 As String, s3 As String
    Dim s4 As String, arr2, arr1, lon
    Dim b As Integer, c As Integer
        c = 6
        s12 = getstr(htmlcode2, "<td>", "</td>", 2)
        arr2 = Split(s12, "分隔符")
       lon = UBound(arr2) ' - LBound(arr)  '获取数组大小
'        For b = 1 To lon / 8
'
'            If Replace(arr2(lon - b - 1 + 0), "<td>", "") = "150" Then
'              c = 1
'              Exit For
'            End If
'
'        Next b
      
        If lon < 1 Then GoTo aa
        
        If lon > 4 Then    '避免没变量下标越界
          Cells(i, 2) = Replace(arr2(lon - 4), "<td>", "") '获取有无完成结果
          Cells(i, 3) = Replace(arr2(lon - 3), "<td>", "") '获取有无完成结果
          Cells(i, 4) = Replace(arr2(lon - 2), "<td>", "") '获取有无完成结果
          Cells(i, 5) = Replace(arr2(lon - 1), "<td>", "")  '获取有无完成结果
          Cells(i, 6) = Replace(arr2(lon - 0), "<td>", "")  '获取有无完成结果
          Else
          GoTo aa
          End If
          If lon > 10 Then
         
      
          Cells(i, 7) = Replace(arr2(lon - 9), "<td>", "")  '获取有无完成结果
          Cells(i, 8) = Replace(arr2(lon - 8), "<td>", "") '获取有无完成结果
          Cells(i, 9) = Replace(arr2(lon - 7), "<td>", "") '获取有无完成结果
          Cells(i, 10) = Replace(arr2(lon - 6), "<td>", "") '获取有无完成结果
          Cells(i, 11) = Replace(arr2(lon - 5), "<td>", "")   '获取有无完成结果
             Else
          GoTo aa
         End If
         If lon > 15 Then
          Cells(i, 12) = Replace(arr2(lon - 14), "<td>", "") '获取有无完成结果
          Cells(i, 13) = Replace(arr2(lon - 13), "<td>", "") '获取有无完成结果
          Cells(i, 14) = Replace(arr2(lon - 12), "<td>", "") '获取有无完成结果
          Cells(i, 15) = Replace(arr2(lon - 11), "<td>", "") '获取有无完成结果
          Cells(i, 16) = Replace(arr2(lon - 10), "<td>", "") '获取有无完成结果
             Else
          GoTo aa
         End If
        Cells(i, 17) = Replace(arr2(1), "<td>", "") '获取有无完成结果
        Cells(i, 18) = Replace(arr2(3), "<td>", "") '获取有无完成结果
        Cells(i, 19) = Replace(arr2(5), "<td>", "") '获取有无完成结果
        Cells(i, 20) = Replace(arr2(6), "<td>", "") '获取有无完成结果
        Cells(i, 21) = Replace(arr2(8), "<td>", "") '获取有无完成结果
        Cells(i, 22) = Replace(arr2(10), "<td>", "") '获取有无完成结果
        Cells(i, 23) = Replace(arr2(11), "<td>", "") '获取有无完成结果
        Cells(i, 24) = Replace(arr2(13), "<td>", "") '获取有无完成结果
        Cells(i, 25) = Replace(arr2(15), "<td>", "") '获取有无完成结果
         
         
aa:
         
          prgramBarShow.Show 0
         
          If i > 30 Then
          Application.ScreenUpdating = False
          End If
         
          prgramBarShow.lblprogress.Width = prgramBarShow.lblBack.Width * i / rowa2
          prgramBarShow.percert.Caption = Format(Round(i / rowa2 * 100, 2), "0") & "%"
          prgramBarShow.Repaint
          If i / rowa2 = 1 Then
           Unload prgramBarShow
           End If

      

  Next i
'End Select
DoEvents



End Sub



Function getstr(sr As String, startstr As String, endstr As String, k As Integer)
'Dim regx As New RegExp ,这个函数 , 是取 开头和结尾之间的  字符
'截取  XX开头  XX 结尾 之间的  字符
Dim mat
Dim m
Set regex = CreateObject("VBScript.RegExp")
With regex
    .Global = True
    .Pattern = "(?=" & startstr & ")[\s\S]*?(?=" & endstr & ")"
     If .test(sr) = False Then
       getstr = "none"
    Else
        If k = 1 Then
            Set mat = .Execute(sr)
            If mat.Count = 1 Then
                getstr = .Execute(sr)(0)
            Else
            For Each m In mat
                If InStr(m, "徐州") > 0 Then getstr = m
            Next
            End If
        ElseIf k = 2 Then
            Set mat = .Execute(sr)
            For Each m In mat
                sr1 = sr1 & m & "分隔符"
            Next
            getstr = Left(sr1, Len(sr1) - 3)
             Set objXML = Nothing
        End If
    End If
End With
End Function
Function getHTML(sdata)
Dim objXML, url, url1, url2
Set objXML = CreateObject("Msxml2.ServerXMLHTTP")
With objXML
    .Open "post", "http://waybill.jd.com/waybill/trackInfoByCode", 0
    .SetRequestHeader "Content-Length", Len(sdata)
    .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    url = "code=" & sdata
    url1 = ""
    url2 = url & url1
    .SetRequestHeader "cookie", ""
    .send url2
   
getHTML = .responsetext
End With
Set objXML = Nothing
End Function

Private Sub 整理格式1()
Sheets("查询界面").Range("A3:B65536").ClearContents
Sheets("查询界面").Range("C3:Y65536").ClearContents

End Sub


TA的精华主题

TA的得分主题

发表于 2023-3-30 18:34 | 显示全部楼层
你这是请教了个寂寞啊

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-30 21:54 来自手机 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2023-3-31 19:26 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 12:30 , Processed in 0.039129 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表