如何用WebBrowser控件取得以下内容

tianhuo_soft 2006-02-05 02:56:53
部分html

<td width='295'><a href=http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm class='blue14' target='_blank'>中国维和警察将承担海地大选计票中心保卫任务</a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/mil/2006-02/05/content_4137547.htm class='blue14' target='_blank'>机密文件显示英国5月将撤回2千名驻伊士兵<font color=green>组图</font></a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/society/2006-02/05/content_4137165.htm class='blue14' target='_blank'>我国华北江淮等地将出现雨雪天 京津等有小雪</a></td>

  我想获取"中国维和警察将承担海地大选计票中心保卫任务"和"机密文件显示英国5月将撤回2千名驻伊士兵"这样的内容.和他们相对应的如http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm我已经获取到.
...全文
给本帖投票
310 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
vansoft 2006-02-06
  • 打赏
  • 举报
回复
northwolves(狼行天下)

同意其意見,用HTML對象來分析,
昨晚才給群裏的一個朋友寫了一個示例。

技術支持:Van_flf@hotmail.com
happy61 2006-02-06
  • 打赏
  • 举报
回复
用mid$ instr
这2个函数
ZOU_SEAFARER 2006-02-06
  • 打赏
  • 举报
回复
Private Sub Source()
Dim i As Integer
For Each objIE In dWinFolder
If objIE.LocationURL = "你需要取内容的网页" Then
Set objDoc = objIE.Document
For i = 1 To objDoc.All.Length - 1
If objDoc.All(i).tagname = "BODY" Then
Text1.Text = objDoc.All(i).innerText
End If
Next
End If
Next
End Sub
这个是取网页内容的函数
anticlimax 2006-02-06
  • 打赏
  • 举报
回复

Dim objRE As New RegExp
Dim objMatches As MatchCollection
Dim objMatch As Match
Dim strExample As String
Dim UrlCodes As String
Private Sub Form_Load()
UrlCodes = "<td width='295'><a href=http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm class='blue14' target='_blank'>中国维和警察将承担海地大选计票中心保卫任务</a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/mil/2006-02/05/content_4137547.htm class='blue14' target='_blank'>机密文件显示英国5月将撤回2千名驻伊士兵<font color=green>组图</font></a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/society/2006-02/05/content_4137165.htm class='blue14' target='_blank'>我国华北江淮等地将出现雨雪天 京津等有小雪</a></td>"
objRE.Pattern = "(<a href=)(.*?)( class='blue14' target='_blank'>)(.*?)(</a>)"
objRE.Global = True
objRE.IgnoreCase = True
strExample = UrlCodes
objRE.Test strExample
Set objMatches = objRE.Execute(strExample)

For Each objMatch In objMatches
MsgBox objMatch.SubMatches(1) & objMatch.SubMatches(3)
Next

Set objMatches = Nothing
Set objRE = Nothing
End Sub
anticlimax 2006-02-06
  • 打赏
  • 举报
回复
为什么不用正则呢?这是解决此类问题效率最高得方法
rainstormmaster 2006-02-05
  • 打赏
  • 举报
回复
顺便说一句:
这是你关于这个问题的第3个帖子了吧,其实没必要发这么多帖子,如果问题没解决,或是有了新的疑问,你接着回复就可以了,关键是要及时回帖
rainstormmaster 2006-02-05
  • 打赏
  • 举报
回复
//可是这个网页 的HMLT代码太多了

这个网页不仅代码多,关键是使用了frame,所以我代码中的IE_DownloadComplete实际上会执行多次
rainstormmaster 2006-02-05
  • 打赏
  • 举报
回复
Option Explicit
Dim WithEvents IE As InternetExplorer
Dim MHtml As HTMLDocument
Private Sub Form_Load()
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "news.xinhuanet.com"
End Sub

Private Sub IE_DownloadComplete()
Set MHtml = IE.document
Dim i As Long, s As String
Dim a As HTMLHtmlElement
For Each a In MHtml.All
If a.nodeName = "A" Then
If Len(a.outerText) = 0 Then
s = "没有文字描述"
Else
s = "文字描述为:" + a.outerText
End If
List1.AddItem s
List1.AddItem "对应href为:" + a.href
End If
Next
End Sub

IamDeane 2006-02-05
  • 打赏
  • 举报
回复
字符串的读取问题
迈克揉索芙特 2006-02-05
  • 打赏
  • 举报
回复
我测试成功了,不求别的。就要分,呵呵。
tianhuo_soft 2006-02-05
  • 打赏
  • 举报
回复
是的
northwolves 2006-02-05
  • 打赏
  • 举报
回复
try:

MsgBox WebBrowser1.Document.body.innertext
northwolves 2006-02-05
  • 打赏
  • 举报
回复
万分感谢  可是这个网页 的HMLT代码太多了  而且class='blue14' "<font color=green>"target='_blank'>" 又N多.
  
   我已经有href了 在webbrowser控件中应该有这样的属性 等待中
-------------------------
你想取出所有的说明?
northwolves 2006-02-05
  • 打赏
  • 举报
回复
Private Sub Command1_Click()

Dim html As String

html = "<td width='295'><a href=http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm class='blue14' target='_blank'>中国维和警察将承担海地大选计票中心保卫任务</a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/mil/2006-02/05/content_4137547.htm class='blue14' target='_blank'>机密文件显示英国5月将撤回2千名驻伊士兵<font color=green>组图</font></a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/society/2006-02/05/content_4137165.htm class='blue14' target='_blank'>我国华北江淮等地将出现雨雪天 京津等有小雪</a></td>"

MsgBox Split(Split(html, "class='blue14' target='_blank'>")(1), "</a>")(0)

End Sub
tianhuo_soft 2006-02-05
  • 打赏
  • 举报
回复
万分感谢  可是这个网页 的HMLT代码太多了  而且class='blue14' "<font color=green>"target='_blank'>" 又N多.
  
   我已经有href了 在webbrowser控件中应该有这样的属性 等待中
faysky2 2006-02-05
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim str As String, strRlt As String, strStart As String, strEnd As String
str = "<td width='295'><a href=http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm class='blue14' target='_blank'>中国维和警察将承担海地大选计票中心保卫任务</a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/mil/2006-02/05/content_4137547.htm class='blue14' target='_blank'>机密文件显示英国5月将撤回2千名驻伊士兵<font color=green>组图</font></a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/society/2006-02/05/content_4137165.htm class='blue14' target='_blank'>我国华北江淮等地将出现雨雪天 京津等有小雪</a></td>"

strStart = "<a href=http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm class='blue14' target='_blank'>"
strEnd = "</a>"

strRlt = findString(str, strStart, strEnd)
Debug.Print strRlt

strStart = "<a href=http://news.xinhuanet.com/mil/2006-02/05/content_4137547.htm class='blue14' target='_blank'>"
strEnd = "<font color=green>"

strRlt = findString(str, strStart, strEnd)
Debug.Print strRlt
End Sub

'写成函数,方便调用
Private Function findString(ByVal str As String, ByVal strStart As String, ByVal strEnd As String) As String
Dim pStart As Long, pEnd As Long
pStart = InStr(str, strStart) + Len(strStart)
pEnd = InStr(pStart, str, strEnd)
findString = Mid(str, pStart, pEnd - pStart)
End Function
faysky2 2006-02-05
  • 打赏
  • 举报
回复
'取得"中国维和警察将承担海地大选计票中心保卫任务"内容(取 class='blue14' target='_blank'> 和 </a> 之间的内容)
'及"机密文件显示英国5月将撤回2千名驻伊士兵"
Private Sub Command3_Click()
Dim str As String, pStart As Long, pEnd As Long, strRlt As String, strStart As String, strEnd As String
str = "<td width='295'><a href=http://news.xinhuanet.com/world/2006-02/05/content_4137572.htm class='blue14' target='_blank'>中国维和警察将承担海地大选计票中心保卫任务</a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/mil/2006-02/05/content_4137547.htm class='blue14' target='_blank'>机密文件显示英国5月将撤回2千名驻伊士兵<font color=green>组图</font></a></td> </tr><tr> <td height='22' width='15' align='center' class='o12'>·</td> <td width='295'><a href=http://news.xinhuanet.com/society/2006-02/05/content_4137165.htm class='blue14' target='_blank'>我国华北江淮等地将出现雨雪天 京津等有小雪</a></td>"

strStart = "class='blue14' target='_blank'>"
strEnd = "</a>"
pStart = InStr(str, strStart)

pEnd = InStr(pStart, str, strEnd)

strRlt = Mid(str, pStart + Len(strStart), pEnd - pStart - Len(strStart))
Debug.Print strRlt

strStart = "class='blue14' target='_blank'>"
strEnd = "<font color=green>"
pStart = InStr(pEnd, str, strStart)

pEnd = InStr(pStart, str, strEnd)

strRlt = Mid(str, pStart + Len(strStart), pEnd - pStart - Len(strStart))
Debug.Print strRlt
End Sub
faysky2 2006-02-05
  • 打赏
  • 举报
回复
用Instr和Mid函数
tianhuo_soft 2006-02-05
  • 打赏
  • 举报
回复
那应该用什么语句  获得呢
Wonny 2006-02-05
  • 打赏
  • 举报
回复
取 class='blue14' target='_blank'> 和 </a> 之间的内容。

7,784

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部