ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

【求助】VBA网抓的问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-2-11 10:12 | 显示全部楼层
本帖最后由 约定的童话 于 2021-2-11 10:54 编辑
389004101 发表于 2021-2-11 10:05
我把扩展名改了,现在可以了。麻烦您下载了改回.xlsm

地址是对的,要用With CreateObject("WinHttp.WinHttpRequest.5.1")配合setRequestHeader "Cookie"进行数据获取

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-11 10:13 | 显示全部楼层
http://www.cwl.gov.cn/cwl_admin/ ... q&issueCount=30
这个源代码用正则很好匹配。现在的问题是VBA抓不到源代码 4.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-11 10:15 | 显示全部楼层

那应该用什么方法呢?
确实,直接复制到浏览器也是打不开的;但是从链接跳转进去是有内容的,不明白什么原因,这方面我完全是小白。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-11 10:42 | 显示全部楼层

那需要怎样修改呢?我怎么回复不了了。说要审核。

TA的精华主题

TA的得分主题

发表于 2021-2-11 11:09 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-11 11:18 | 显示全部楼层
约定的童话 发表于 2021-2-11 10:12
地址是对的,要用With CreateObject("WinHttp.WinHttpRequest.5.1")配合setRequestHeader "Cookie"进行数 ...

我刚才百度了一下,是不是这种格式的?
  1. Dim oXMLHttpRequest As Object
  2. Set oXMLHttpRequest = CreateObject("Microsoft.XmlHttp")
  3. oXMLHttpRequest.setRequestHeader "Accept", "text/html, application/xhtml+xml, */*"
  4. oXMLHttpRequest.setRequestHeader "Accept-Language", "ko-KR"
  5. oXMLHttpRequest.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  6. oXMLHttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  7. oXMLHttpRequest.setRequestHeader "Accept-Encoding", "gzip, deflate"
  8. oXMLHttpRequest.setRequestHeader "Connection", "Keep-Alive"
  9. oXMLHttpRequest.setRequestHeader "DNT", "1"
  10. oXMLHttpRequest.setRequestHeader "Cookie", "xxx=yyy"
  11. oXMLHttpRequest.send[enter image description here][1]
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-11 11:44 | 显示全部楼层
  1.     Set myHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
  2.     With myHTTP
  3.         .Open "GET", "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=30", False
  4.         .setRequestHeader "Host", "www.cwl.gov.cn"
  5.         .setRequestHeader "Connection", "keep-alive"
  6.         .setRequestHeader "Upgrade-Insecure-Requests", "1"
  7.         .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400"
  8.         .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  9.         .setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
  10.         .setRequestHeader "Referer", "http://club.excelhome.net/thread-1575009-1-1.html"
  11.         .setRequestHeader "Accept-Encoding", "gzip, deflate"
  12.         .setRequestHeader "Accept-Language", "zh-CN,zh;q=0.9"
  13.         .setRequestHeader "Cookie", "_ga=GA1.3.1058570018.1612955503; _gid=GA1.3.1852979334.1612955503; 21_vq=26"
  14.         .send
  15.     End With
复制代码

哈哈哈哈,依葫芦画瓢,成功了!!!!!

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-11 12:11 | 显示全部楼层
放出全部代码:
  1. Sub wzssqkj()
  2.     Dim myHTTP As Object, s As String
  3.    
  4.     'Set myHTTP = CreateObject("Microsoft.XmlHttp")
  5.     'myHTTP.Open "GET", "http://datachart.500.com/ssq/history/newinc/history.php?limit=30&sort=0", False
  6.     'myHTTP.send
  7.     Set myHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
  8.     With myHTTP
  9.         .Open "GET", "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=30", False
  10.         .setRequestHeader "Host", "www.cwl.gov.cn"
  11.         .setRequestHeader "Connection", "keep-alive"
  12.         .setRequestHeader "Upgrade-Insecure-Requests", "1"
  13.         .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400"
  14.         .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  15.         .setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
  16.         .setRequestHeader "Referer", "http://club.excelhome.net/thread-1575009-1-1.html"
  17.         .setRequestHeader "Accept-Encoding", "gzip, deflate"
  18.         .setRequestHeader "Accept-Language", "zh-CN,zh;q=0.9"
  19.         .setRequestHeader "Cookie", "_ga=GA1.3.1058570018.1612955503; _gid=GA1.3.1852979334.1612955503; 21_vq=26"
  20.         .send
  21.     End With
  22.    
  23.     s = myHTTP.responsetext

  24.     Dim regex As Object, mches As Object, mch As Object, i&, j&

  25.     Set regex = CreateObject("VBScript.Regexp")
  26.     regex.Global = True
  27.     regex.Pattern = "code"":""(\d+).*?date"":""(.*?)"".*?(\d\d),(\d\d),(\d\d),(\d\d),(\d\d),(\d\d)"".*?(\d\d).*?typemoney"":""(\d+).*?typemoney"":""(\d+)"
  28.    
  29.     Set mches = regex.Execute(s)
  30.    
  31.     i = 2
  32.     For Each mch In mches
  33.         For j = 0 To 10
  34.             Sheet7.Cells(i, j + 1) = mch.submatches(j)
  35.         Next j
  36.         i = i + 1
  37.     Next mch
  38. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2021-2-12 13:20 | 显示全部楼层
Sub wzssqkj()
  Dim url As String, s As String, oH As Object, ar(1 To 56789, 1 To 10), k As Long
  url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount="
  With CreateObject("WinHttp.WinHttpRequest.5.1")
    .Open "GET", url & 50, False
    .setRequestHeader "Referer", "http://www.cwl.gov.cn/"
    .Send
    s = .ResponseText
  End With
  Set oH = CreateObject("htmlfile")
  GetJsonData oH.parentWindow, s, ar, k
  Cells.Clear
  Range("A2").Resize(k, 10) = ar
  Set oH = Nothing
End Sub

Function GetJsonData(oW As Object, s As String, ar(), k As Long)
  Dim i&, j&, n&, t
  With oW
    .execScript "var a=" & s & "['result'];"
    j = .eval("a.length")
    For i = 0 To j - 1
      k = k + 1
      ar(k, 1) = .eval("a[" & i & "]['code']")
      ar(k, 2) = .eval("a[" & i & "]['date']")
      t = Split(.eval("a[" & i & "]['red']"), ",")
      For n = 0 To UBound(t)
        ar(k, 3 + n) = t(n)
      Next
      ar(k, 8) = .eval("a[" & i & "]['blue']")
      ar(k, 9) = .eval("a[" & i & "]['prizegrades'][0]['typemoney']")
      ar(k, 10) = .eval("a[" & i & "]['prizegrades'][1]['typemoney']")
    Next
  End With
End Function

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-12-27 11:01 , Processed in 0.047330 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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