VB读取网页内容 方法汇总

上一篇 / 下一篇  2012-08-03 16:48:39 / 个人分类:开发

VB读取网页内容 方法汇总  

2011-02-27 12:41:37|  分类: 编程源码 |  标签:dim  网页  sub  text  msgbox  |字号 订阅

工作需要!有个网页有很多的内容,要读取!  首先网页上有很多标题,每个标题对应着相应的网页,要把这个相应的网页内容读出来,怎么做?不知说清楚没!举个例子吧!  比如xxx.xxx.xxx.xx这是一个网页,这个网页里只显示标题,如:  1,xxxxxxx  2,xxxxx  3xxxx  4xxxx  有很多页,而每个标题又对应相应的网页,这个网页里的内容要把它读出来。要怎么做呢?谢谢!

问题补充:

非常感谢你的回答!可以获取内容,但现在又有新的问题,获取的是整个网页的内容,我只要里面<p>--</p>之间的内容,要怎么弄呢?谢谢!


ik.qb.data.add('page','fromWap','0'); 
最佳答案 
这里有两中方法,个人觉得第一种比较快,而且第二种字体总显示乱码。   1、利用inet控件   放一个Internet Transfer Control,一个按纽和两个文本框在窗体上   text1用来输入网址,text2用来输入暂存的文件名如c:\temp.htm   然后输入以下代码   Private Sub Command1_Click()   Dim B() As Byte   '取消所有操作   Inet1.Cancel   '设定协议为HTTP   Inet1.Protocol = icHTTP   '设定URL属性   Inet1.URL = Text1   '将读取的HTML数据放进一个byte array   B() = Inet1.OpenURL(, icByteArray)   '建立一个暂存文件来存放取回来的html文件   Open Text2 For Binary Access Write As #1       Put #1, , B()   Close #1   MsgBox "ok"   End Sub   2、利用webbrower控件   和上边放一样的控件,不过把inet改成webbrower控件(引用的时候选internet controls就可以了),另外再加一个timer控件   然后加入以下代码   Private Sub Command1_Click()   WebBrowser1.Navigate Text1   Timer1.Enabled = True   End Sub     Private Sub Timer1_Timer()   Dim Doc, ObjHtml As Object   Dim StrHtml As String   Dim B() As Byte   If Not WebBrowser1.Busy Then       Set Doc = WebBrowser1.Document       Set bjHtml = Doc.body.createtextrange()       If Not IsNull(ObjHtml) Then           B() = ObjHtml.htmltext           Open "c:\temp.htm" For Binary Access Write As #1               Put #1, , B()           Close #1       End If       Timer1.Enabled = False       MsgBox "ok"   End If   End Sub     朋友你好,你现在会读HTML了,但是现在要读取指定标签里的内容,比如<p>中的,你可以这样:    放一个WebBrowser(引用部件的Microsoft Internet Controls)  然后复制下面的代码:    Private Sub Form_Load()      WebBrowser1.Navigate2 "那个网页的地址/文件地址"  End Sub    Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)      Dim doc As Object, i      Set doc = WebBrowser1.Document      For Each i In doc.getelementsbytagname("p")          Text1.Text = Text1.Text & " " & i.innertext  '得到所有p标签元素;然后将内容(非HTML,这样过滤H5标签)加入文本框      Next  End Sub        '祝楼主你成功喔!

VB.NET:读取网页的方法

网页抓取及下载 2008-10-31 01:35:21 阅读42 评论0   字号: 订阅

用 HttpWebResponse 相关类 GET 和 POST 信息

一、用GET方式读取网页源代码:

Dim httpReq As System.Net.HttpWebRequest     'HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持,也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。 
Dim httpResp As System.Net.HttpWebResponse 
Dim url as String=“ http://www.Tuenhai.com  'tuenhai的小站
Dim httpURL As New System.Uri(url)    
httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest) 
httpReq.Method = "GET" 
httpResp = CType(httpReq.GetResponse(), HttpWebResponse) 
Dim reader As StreamReader = _ 
New StreamReader(httpResp.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))     ' 如是中文,要设置编码格式为 "GB 2312" 。 
Dim respHTML As String = reader.ReadToEnd() 'respHTML 就是网页源代码 
httpResp.Close()  

 

二、向服务器 POST 信息:

Dim httpUrl2 As New System.Uri(" http://www.Tuenhai.com?"& "name=1&pass= 6" ) ' "&" 号后面是' 注册信息,改成你自己的 
Dim req As HttpWebRequest 
req = CType(WebRequest.Create(httpUrl2) , HttpWebRequest) 
req.Method = "POST" 
req.ContentType = "application/x-www-form-urlencoded" 
Dim bytesData() As Byte = System.Text.Encoding.ASCII.GetBytes("name=1&pass= 6") 
req.ContentLength = bytesData.Length 
Dim postStream As Stream = req.GetRequestStream() 
postStream.Write(bytesData, 0 , bytesData.Length) ' 以上为向网络服务器 POST 信息

Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) 
Dim reader As StreamReader = _ 
New StreamReader(res.GetResponseStream , System.Text.Encoding.GetEncoding("GB2312")) 
Dim respHTML As String = reader.ReadToEnd() 'respHTML 为 POST 后网络服务器返回的信息 
MsgBox(respHTML) ' 可用 MsgBox 查看返回的信息 
res.Close() 

 

三、用正则表达式从网页源代码中提取网址

  得到网页源代后,我们可以对源代码进行处理,比如提取其中的链接, Email 地址,图片地址, Flash 等等,这在论坛自动发贴软件工具中很有用。

Dim r As System.Text.RegularExpressions.Regex 
Dim m As System.Text.RegularExpressions.MatchCollection 
Dim respHtml As String =" http://www.NETsh.Net/subdomains/f_s_o.php?leibie=shangmao "' 以这个地址举例 
strRegex ="http://([\w]+\.)+[\w]+(/[\w.?]+)+leibie[=]{1}[\w]+" ' 用这个正则表达式可以提取上面地址。通用 ' 的提取链接正则表达式为 "http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? "。   
r = New System.Text.RegularExpressions.Regex(strRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase) 
m = r.Matches(respHTML) 
Dim i As Integer 
For i = 0 To m.Count - 1 
    MsgBox(m(i).Value) 
Next i 

 

vb读取网页源码
━━━━━━━━━━━━━━━━━━━━━━━━━━

Private Sub Command1_Click()
    Dim Inet As Object
    Dim s As String
    Set Inet = CreateObject("InetCtls.Inet")
    Inet.RequestTimeOut = 20
    Inet.Url = "http://www.google.com.tw"
    s = Inet.OpenURL
    MsgBox s
End Sub


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-04  
   1234
567891011
12131415161718
19202122232425
262728293031 

我的存档

数据统计

  • 访问量: 5588
  • 日志数: 2
  • 建立时间: 2012-08-02
  • 更新时间: 2012-08-09

RSS订阅

Open Toolbar