Vb.net 基于VB的Web解析问题
我对VB.NET非常陌生,目前正在学习如何抓取和解析网站。简而言之,我的问题是——如果我在代码中多次使用“getElementsByClassName”,它将只在第一次工作。与“getElementsByTagName”的情况相同。即使我只是手动解析html代码,它也只能在第一次工作 下面是一个使用“getElementsByClassName”的示例。我有带按钮1和列表框1的Form1。我试图从两个网站(谷歌和英国广播公司)获取新闻标题,然后将它们放入列表框1。您可以看到我将代码分为两部分。我想指出的是,这两个部分都很好地工作,并获得了我需要的信息,但只有在单独使用时。当像下面的例子一样组合在一起时,第一部分(Google)将毫无问题地执行,但第二部分(BBC)将在“Dim AllItemsBBC As Object=SecondBrowser.Document.getElementsByClassName(“title-link_utitle-text”)”行上给出一个错误 现在更有趣的是,如果我翻转代码,将BBC部分放在第一位,Google放在第二位,BBC将毫无问题地执行,Google将在“Dim AllItemsGoogle As Object=FirstBrowser.Document.getElementsByClassName(“titletext”)”行上给我错误。基本上,第一个执行时没有问题,第二个失败 错误消息显示“Microsoft.VisualBasic.dll中发生“System.NotSupportedException”类型的未处理异常。其他信息:来自HRESULT的异常:0x800A01B6” 例1:Vb.net 基于VB的Web解析问题,vb.net,web-scraping,Vb.net,Web Scraping,我对VB.NET非常陌生,目前正在学习如何抓取和解析网站。简而言之,我的问题是——如果我在代码中多次使用“getElementsByClassName”,它将只在第一次工作。与“getElementsByTagName”的情况相同。即使我只是手动解析html代码,它也只能在第一次工作 下面是一个使用“getElementsByClassName”的示例。我有带按钮1和列表框1的Form1。我试图从两个网站(谷歌和英国广播公司)获取新闻标题,然后将它们放入列表框1。您可以看到我将代码分为两部分。我
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'START OF PART 1
'Creating and navigating the IE browser to Google news page
Dim FirstBrowser As Object = CreateObject("InternetExplorer.Application")
FirstBrowser.Visible = True
FirstBrowser.Navigate("https://news.google.com/news?cf=all&pz=1&ned=us")
Do
Application.DoEvents()
Loop Until FirstBrowser.readyState = 4
'Getting the titles from Google news page and adding them to ListBox1
Dim ItemGoogle As Object
Dim AllItemsGoogle As Object = FirstBrowser.Document.getElementsByClassName("titletext")
For Each ItemGoogle In AllItemsGoogle
ListBox1.Items.Add(ItemGoogle.InnerText)
Next ItemGoogle
'Closing the browser
FirstBrowser.Quit()
'END OF PART1
'START OF PART 2
'Creating and navigating the IE browser to BBC news page
Dim SecondBrowser As Object = CreateObject("InternetExplorer.Application")
SecondBrowser.Visible = True
SecondBrowser.Navigate("http://www.bbc.com/news")
Do
Application.DoEvents()
Loop Until SecondBrowser.readyState = 4
'Getting the titles from BBC news page and adding them to ListBox1
Dim ItemBBC As Object
Dim AllItemsBBC As Object = SecondBrowser.Document.getElementsByClassName("title-link__title-text")
For Each ItemBBC In AllItemsBBC
ListBox1.Items.Add(ItemBBC.InnerText)
Next ItemBBC
'Closing the browser
SecondBrowser.Quit()
'END OF PART 2
End Sub
End Class
我的第二个例子是我解析相同的网站,基本上只是找到我需要的短语。同样的情况下,谷歌部分工作,BBC在“Dim the_html_code_BBC As String=SecondBrowser.Document.Body.InnerHTML”一行失败
把它翻过来,BBC工作,谷歌在“Dim the_html_code_Google As String=FirstBrowser.Document.Body.InnerHTML”一行失败
错误消息显示“Microsoft.VisualBasic.dll中发生了类型为“System.MissingMemberException”的未处理异常。其他信息:未找到类型为“JScript TypeInfo”的公共成员“InnerHTML”
例2
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'START OF PART 1
'Creating and navigating the IE browser to Google news page
Dim FirstBrowser As Object = CreateObject("InternetExplorer.Application")
FirstBrowser.Visible = True
FirstBrowser.Navigate("https://news.google.com/news?cf=all&pz=1&ned=us")
Do
Application.DoEvents()
Loop Until FirstBrowser.readyState = 4
'Getting the titles from Google news page and adding them to ListBox1
Dim the_html_code_google As String = FirstBrowser.Document.Body.InnerHTML
Dim start_of_code_google As String
Dim code_selection_google As String
Do
Application.DoEvents()
start_of_code_google = InStr(the_html_code_google, "titletext")
If start_of_code_google > 0 Then
code_selection_google = Mid(the_html_code_google, start_of_code_google + 11, Len(the_html_code_google))
the_html_code_google = Mid(the_html_code_google, start_of_code_google + 11, Len(the_html_code_google))
code_selection_google = Mid(code_selection_google, 1, InStr(code_selection_google, Chr(60)) - 1)
ListBox1.Items.Add(code_selection_google)
End If
Loop Until start_of_code_google = 0
'Closing the browser
FirstBrowser.Quit()
'END OF PART1
'START OF PART 2
'Creating and navigating the IE browser to BBC news page
Dim SecondBrowser As Object = CreateObject("InternetExplorer.Application")
SecondBrowser.Visible = True
SecondBrowser.Navigate("http://www.bbc.com/news")
Do
Application.DoEvents()
Loop Until SecondBrowser.readyState = 4
'Getting the titles from BBC news page and adding them to ListBox1
Dim the_html_code_bbc As String = SecondBrowser.Document.Body.InnerHTML
Dim start_of_code_bbc As String
Dim code_selection_bbc As String
Do
Application.DoEvents()
start_of_code_bbc = InStr(the_html_code_bbc, "title-link__title-text")
If start_of_code_bbc > 0 Then
code_selection_bbc = Mid(the_html_code_bbc, start_of_code_bbc + 24, Len(the_html_code_bbc))
the_html_code_bbc = Mid(the_html_code_bbc, start_of_code_bbc + 24, Len(the_html_code_bbc))
code_selection_bbc = Mid(code_selection_bbc, 1, InStr(code_selection_bbc, Chr(60)) - 1)
ListBox1.Items.Add(code_selection_bbc)
End If
Loop Until start_of_code_bbc = 0
'Closing the browser
SecondBrowser.Quit()
'END OF PART 2
End Sub
End Class
另一件值得一提的事情是,如果我对谷歌部分使用一种解析方法,对BBC使用另一种解析方法,那么一切都很好
由于缺乏Visual Studio的经验,我一定错过了一些东西。我正在使用Windows桌面版的Express 2013。如果您知道导致此问题的原因,我将非常感谢您的建议。这与您正在执行的后期绑定有关…使用
对象而不是显式定义的类型。我建议1)不要进行后期绑定,2)使用webbrowser控件而不是创建IE实例。您可以使用webbrowser以类似的方式访问整个文档。感谢您的帮助。我采用了第二种解决方案,比我所做的要实际得多。