vbnet取網頁源碼
1. 如何用vb.net獲得網頁的源代碼
Dim url As String=" 網址"
Dim httpReq As System.Net.HttpWebRequest
Dim httpResp As System.Net.HttpWebResponse
Dim httpURL As New System.Uri(url)
httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
httpReq.Method = "GET"
httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
httpReq.KeepAlive = False ' 獲取或設置一個值,該值指示是否與
Internet資源建立持久連接。
Dim reader As StreamReader = _
New StreamReader(httpResp.GetResponseStream,
System.Text.Encoding.GetEncoding(-0))
Dim respHTML As String = reader.ReadToEnd() 'respHTML就是網頁源代碼
2. VB.NET如何使用正則表達式讀取網頁源碼中的指定內容
<divclass="cont">[sS]*?href=["']?([^"']*)["']?[sS]*?title=["']?([^"]*)["']?
3. VB.NET 不使用控制項獲取某網頁源代碼
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyClient As Net.WebClient = New Net.WebClient
Dim MyReader As New System.IO.StreamReader(MyClient.OpenRead("http://wap..com"), System.Text.Encoding.Default)
Dim MyWebCode As String = MyReader.ReadToEnd
Me.RichTextBox1.Text = MyWebCode
MyReader.Close()
End Sub
4. vb.net中如何利用api函數獲取網頁源代碼
Dim MyClient As WebClient = New WebClient
Dim MyReader As New System.IO.StreamReader(MyClient.OpenRead(url), System.Text.Encoding.Default) '定義新的文件流並讀取網頁文件數據,url表示需要打開的網頁地址
Dim longTxt As String = MyReader.ReadToEnd 'longtxt存儲了網頁的源碼
MyReader.Close()
5. vb 快速的取得網頁源碼和提取其中內容
如下函數可以實現你要的功能:
Function TestRegExp(ByVal myString$, ByVal myPattern$, Optional myMark$ = "gimt", Optional myRepString$)
If myString = "" Or myPattern = "" Then
TestRegExp = ""
Debug.Print "數據不能為空"
Exit Function
End If
'Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String
Set objRegExp = New RegExp
' 正則式
objRegExp.Pattern = myPattern
'IIf(InStr(myPattern, "s") <> 0, "", myPattern)
' 是否區分大小寫
objRegExp.IgnoreCase = IIf(InStr(LCase$(myMark), "i") > 0, True, False)
' 是否全局
objRegExp.Global = IIf(InStr(LCase$(myMark), "g") > 0, True, False)
' 是否換行
objRegExp.MultiLine = IIf(InStr(LCase$(myMark), "m") > 0, True, False)
If objRegExp.Test(myString) Then
If InStr(myMark, "r") > 0 Then
Debug.Print "正在替換"
RetStr = objRegExp.Replace(myString, myRepString)
Debug.Print IIf(RetStr <> "", "替換結果" & Left(RetStr, 10), "沒搜索到可替換字元")
Else
Debug.Print "正在搜索"
Set colMatches = objRegExp.Execute(myString) ' Execute search.
'MsgBox colMatches.Count
For Each objMatch In colMatches ' Iterate Matches collection.
'RetStr = RetStr & objMatch.FirstIndex
RetStr = RetStr & objMatch.Value & vbCrLf
Next
Debug.Print IIf(RetStr <> "", "搜索結果" & Left(RetStr, 10), "沒搜索到匹配結果")
End If
Else
RetStr = ""
Debug.Print "沒找到任何匹配結果,請檢查正則式的是否正確"
End If
TestRegExp = RetStr
End Function
6. vb.net源碼網站
洪恩在線:http://www.hongen.com/pc/program/codedown/vbcode/
天新網:http://www.21tx.com/src/vb/
VB:http://www.21tx.com/src/vb/