我用vba抓取一些网站以获得乐趣,我使用VBA作为工具.我使用XMLHTTP和HTMLDocument(因为它比internetExplorer.Application更快).
Public Sub XMLhtmlDocumentHTMLSourceScraper() Dim XMLHTTPReq As Object Dim htmlDoc As HTMLDocument Dim postURL As String postURL = "http://foodffs.tumblr.com/archive/2015/11" Set XMLHTTPReq = New MSXML2.XMLHTTP With XMLHTTPReq .Open "GET", postURL, False .Send End With Set htmlDoc = New HTMLDocument With htmlDoc .body.innerHTML = XMLHTTPReq.responseText End With i = 0 Set varTemp = htmlDoc.getElementsByClassName("post_glass post_micro_glass") For Each vr In varTemp ''''the next line is important to solve this issue *1 Cells(1, 1) = vr.outerHTML Set varTemp2 = vr.getElementsByTagName("SPAN class=post_date") Cells(i + 1, 3) = varTemp2.Item(0).innerText ''''the next line occur 438Error'''' Set varTemp2 = vr.getElementsByClassName("hover_inner") Cells(i + 1, 4) = varTemp2.innerText i = i + 1 Next vr End Sub
我通过*1单元格(1,1)向我展示了下一个问题
...............是的所有班级标签丢失了"".只有第一个函数的类有""我真的不知道为什么会出现这种情况.
//我可以通过getElementsByTagName("span")进行解析.但我更喜欢"上课"标签.....
1> 小智..:该getElementsByClassName方法方法不被认为是其自身的方法; 仅限父HTMLDocument.如果要使用它来定位DIV元素中的元素,则需要创建由该特定DIV元素的.outerHtml组成的子HTMLDocument.
Public Sub XMLhtmlDocumentHTMLSourceScraper() Dim xmlHTTPReq As New MSXML2.XMLHTTP Dim htmlDOC As New HTMLDocument, divSUBDOC As New HTMLDocument Dim iDIV As Long, iSPN As Long, iEL As Long Dim postURL As String, nr As Long, i As Long postURL = "http://foodffs.tumblr.com/archive/2015/11" With xmlHTTPReq .Open "GET", postURL, False .Send End With 'Set htmlDOC = New HTMLDocument With htmlDOC .body.innerHTML = xmlHTTPReq.responseText End With i = 0 With htmlDOC For iDIV = 0 To .getElementsByClassName("post_glass post_micro_glass").Length - 1 nr = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row With .getElementsByClassName("post_glass post_micro_glass")(iDIV) 'method 1 - run through multiples in a collection For iSPN = 0 To .getElementsByTagName("span").Length - 1 With .getElementsByTagName("span")(iSPN) Select Case LCase(.className) Case "post_date" Cells(nr, 3) = .innerText Case "post_notes" Cells(nr, 4) = .innerText Case Else 'do nothing End Select End With Next iSPN 'method 2 - create a sub-HTML doc to facilitate getting els by classname divSUBDOC.body.innerHTML = .outerHTML 'only the HTML from this DIV With divSUBDOC If CBool(.getElementsByClassName("hover_inner").Length) Then 'there is at least 1 'use the first Cells(nr, 5) = .getElementsByClassName("hover_inner")(0).innerText End If End With End With Next iDIV End With End Sub虽然其他.getElementsByXXXX可以很容易地检索另一个元素中的集合,但getElementsByClassName方法需要考虑它认为整个HTMLDocument的内容,即使你已经愚弄了它.
推荐阅读
如何解决《为什么我不能插入/插入我的地图?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《在dagre-d3中为节点分配超链接》经验,为你挑选了1个好方法。 ... [详细] 如何解决《我应该如何在typedef中定义一组位数?》经验,为你挑选了0个好方法。 ... [详细] 如何解决《在android中动态设置状态栏颜色》经验,为你挑选了2个好方法。 ... [详细] 如何解决《Android-Skobbler不支持64位处理器》经验,为你挑选了0个好方法。 ... [详细] 如何解决《DIV:全高+滚动》经验,为你挑选了1个好方法。 ... [详细] 如何解决《为什么定义析构函数会删除隐式定义的移动赋值运算符?》经验,为你挑选了2个好方法。 ... [详细] 如何解决《UIImageView在启动屏幕上是黑色的》经验,为你挑选了1个好方法。 ... [详细] 如何解决《传递char**作为参数》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如何与MongoDbC#Driver2.1异步使用AsQueryable方法?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《PolymerJS:Iron-Ajax-如何将令牌绑定到标题属性?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《Shell脚本使用不清楚":"》经验,为你挑选了1个好方法。 ... [详细] 如何解决《我怎样才能确保非法行为是不可行的?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《使用html2canvas将高图图表呈现为pdf不适用于IE和Firefox》经验,为你挑选了1个好方法。 ... [详细] 如何解决《Postgres9.4中JSON->JSONB列转换的速度大致有多快》经验,为你挑选了1个好方法。 ... [详细] 如何解决《Webpack和字体:模块解析失败》经验,为你挑选了1个好方法。 ... [详细] 如何解决《xamarin表单错误Droid.Resource不包含字符串的定义》经验,为你挑选了1个好方法。 ... [详细] 如何解决《对于Lollipop及以上的仪器测试,"没有测试结果"》经验,为你挑选了0个好方法。 ... [详细] 如何解决《如何保存我的应用设置?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《ASP.NET5RC1-Final中不存在UseWindowsAzureActiveDirectoryBearerAuthentication》经验,为你挑选了1个好方法。 ... [详细]吐了个 "CAO" !Tags | 热门标签RankList | 热门文章
- 1Oracle从类型号数组中获取distict值
- 2每次符号在值列表中更改时计算差异
- 3在Javascript中使用reduce来对空数组和非空数组求和
- 4为什么preg_match会返回一些空元素?
- 5不幸的是,相机已停止
- 6展开/折叠动画:小滞后|| MeasureSpec返回错误的值
- 7转换为十进制并执行OrderBy
- 8在c#中捕获鼠标单击事件
- 9Exchange FindItem使用一组商品ID和多个商品ID的不同属性集进行响应
- 10在整个应用程序中禁用UITextField的自动更正
- 11终端&&非终端递归调用
- 12WebGL中多个绘制缓冲区上的readPixels
- 13如何在我的函数场景中使用标签Cucumber.js?
- 14重新排序列表以最大化相邻元素的差异
- 15Flexbox和溢出隐藏不正常
- 16Bootstrap 3.x:点击模态触发器后如何更改网址?
- 17在Salesforce中获取{“ error”:“ invalid_grant”,“ error_description”:“身份验证失败”}?
- 18在java代码中使用scala元组时,Intellij"无法解析符号_1"
- 19通过Java配置通过CommonAnnotationBeanPostProcessor激活@PostConstruct的正确方法是什么?
- 20无法在我的脚本中找到错误的行为不符合预期
DevBox开发工具箱 | 专业的在线开发工具网站 京公网安备 11010802040832号 | 京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有