vb 用 Internet Controls 请求到一网页后,如何取得其 html 内容?
就象用 IE 的 “查看源文件”功能一样看到的文本内容,也就是那些文字与html标记。我需要取得网页的源文件后分析里面的文字,获取需要的资料。
那些文字没有放在控件中(如果在控件中就好引用了),只是用 <td> 、<p> 、<font> 等标记的一些文字,并且不确定格式,只能逐字分析。
就象用 IE 的 “查看源文件”功能一样看到的文本内容,也就是那些文字与html标记。我需要取得网页的源文件后分析里面的文字,获取需要的资料。
那些文字没有放在控件中(如果在控件中就好引用了),只是用 <td> 、<p> 、<font> 等标记的一些文字,并且不确定格式,只能逐字分析。
解决方案 »
- 如何修改VB小程序的复选框为选上(没有源码)
- Excel中画方框出错- 超出范围?
- 关于两表间的取数问题!急!
- 在vb中能不能实现域名(一级和二级)的查询
- Crystal Reports 8.5转PDF文件,如果有中文为何变成乱码?如何解决?谢谢!
- 有谁遇到过这种情况,在ACCESS里建了一个表,ADO绑定时,却出现三个表。
- 请教UDP通讯没反应
- 数据库问题,救救我
- 没有数字签名,如何使VB开发的程序不出现是否信任的对话框?
- (急!)如何将一个自定义的结构数组的头指针传递给.dll中的函数
- DAO的问题??请教高手!
- 请问如何使用VB(DLL)写一个输出流文件(ASP),提示用户下载一个文件,下载完后再删除这个文件
Dim bData() As Byte '数据变量
Dim intFile As Integer '可用文件变量
Dim i As Integer
Dim ttt As String
Inet1.Tag = "Work"
intFile = FreeFile() '将 intFile 设置为未使用的文件
' OpenURL 方法的结果首先传入 Byte 数组,
'然后将 Byte 数组保存到磁盘。
On Error Resume Next
Kill LocalFile
On Error GoTo 0
bData() = Inet1.OpenURL(UrtFile, icByteArray)
Open LocalFile For Binary Access Write As #intFile
Put #intFile, , bData()
Close #intFile
Inet1.Tag = ""
Exit Sub
Err1:
MsgBox "error!"
Resume
End Sub
Private Function strGetWebPageSource(strWebPageHTTP As String, thisInet As Inet) As String
Dim tmpSourceStr As String
'thisInet设置
If theblGetURL Then '正在获取网页源码,不再获取
Exit Function
End If
theblGetURL = True '正在获取网页源码标志
Select Case theiUseDownType
Case 2 '第二种获取源码的方法
theWebPageSource2 = ""
thisInet.Execute strWebPageHTTP, "GET"
Do While True
If theStop Then
theblGetWebPageError = True
Exit Do
End If
If Not (Len(theWebPageSource2) = 0) Then '获取到源码
If theblGetWebPageError Then
tmpSourceStr = ""
Else
tmpSourceStr = theWebPageSource2
End If
Exit Do
End If
DoEvents
Call ZZG_Sleep(1)
Loop
Case Else
'取网页
tmpSourceStr = thisInet.OpenURL(strWebPageHTTP, 0)
If theblGetWebPageError Then
tmpSourceStr = ""
End If
End Select
strGetWebPageSource = tmpSourceStr
theblGetURL = False '获取网页源码结束标志
End Function'Inet设置
Private Function blInetSet(thisInet As Inet) As Boolean
If theProxyIP = "" Then '没有代理
thisInet.AccessType = icUseDefault
thisInet.Proxy = ""
Else
thisInet.AccessType = icNamedProxy
thisInet.Proxy = theProxyIP & ":" & theProxyPort
End If
blInetSet = True
End Function'获取网页时的状态
Private Sub MyInet_StateChanged(ByVal State As Integer)
Dim tmpMessage As String
tmpMessage = "▁▅█☉当前状态:"
Select Case State
Case 1
theMessageText = tmpMessage & "正在查询所指定的主机的 IP 地址"
Case 2
theMessageText = tmpMessage & "成功地找到所指定的主机的 IP 地址"
Case 3
theMessageText = tmpMessage & "正在与主机连接"
Case 4
theMessageText = tmpMessage & "已与主机连接成功"
Case 5
theMessageText = tmpMessage & "正在向主机发送请求"
Case 6
theMessageText = tmpMessage & "发送请求已成功"
Case 7
theMessageText = tmpMessage & "在接收主机的响应"
Case 8
theMessageText = tmpMessage & "成功地接收到主机的响应"
Case 9
theMessageText = tmpMessage & "正在解除与主机的连接"
Case 10
theMessageText = tmpMessage & "已成功地与主机解除了连接"
Case 11
theMessageText = tmpMessage & "与主机通讯时出现了错误"
theWebPageSource2 = "Err"
theblGetWebPageError = True
Case 12
Select Case theiUseDownType
Case 2 'FTP
Dim tmpByte() As Byte
Dim tmpTempFilePath As String
Dim tmpID As Integer
tmpTempFilePath = thePath & "tmpS.tmp"
tmpID = FreeFile
Open tmpTempFilePath For Binary Access Write As #tmpID
tmpByte = MyInet.GetChunk(1024, icByteArray)
Do While UBound(tmpByte) > 0
Put #tmpID, , tmpByte
tmpByte = MyInet.GetChunk(1024, icByteArray)
DoEvents
Loop
Put #tmpID, , tmpByte
Close #tmpID
theWebPageSource2 = GetTextFileToStr(tmpTempFilePath)
Call ZZG_blDeleteFile(tmpTempFilePath)
End Select
theMessageText = tmpMessage & "请求已经完成,并且所有数据均已接收到"
End Select
Call blWriteInfo(theMessageText)
Me.Refresh
End Sub
请问 Inet 控件是什么来着?我用 microsoft internet controls ,即 webbrowser 控件,它没有 openURL 方法。
Dim b() As Byte
'将读取的 HTML 数据放进一个 byte array.
b() = Inet1.OpenURL(, icByteArray)
'建立一个暂存文件來存放取回來的 html 文件
Open Text2.text For Binary Access Write As #1
Put #1, , b()
Close #1
End Sub
这个是我做的, 你可以参考下里面的代码 用的就是 inet 控件
有这2个你应该能做出来了
否则,我可能得考虑换用 inet 控件了。
自动上网抓数据的机器人
http://dev.csdn.net/develop/article/17/17697.shtm
参考
http://dev.csdn.net/article/28/28377.shtm
http://dev.csdn.net/article/28/28376.shtm
http://dev.csdn.net/article/28/28374.shtm
http://dev.csdn.net/article/28/28375.shtmhttp://www.applevb.com/qa/qa000257.htm也不一定要分析文件,可以通过webbrowser的对象来做
否则,我可能得考虑换用 inet 控件了。以前讨论过很多次了,自己在论坛内搜索以下