vb.net如何爬取网页 vbnet抓取网页数据

vb.net如何获取网页的cookie

这个问题有点意思,但题主表意不明,确切的说应该分Web端和客户端两种情况。

专注于为中小企业提供成都做网站、成都网站设计、成都外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业安丘免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

想来问到这个问题的不应该是开发Web端的,说说客户端的吧:

1、HttpWebRequest.CookieContainer

Cookies通过HttpResponse传给客户端,通过HttpRequest传回服务端,因此你可以

设置Response.Cookies集合的值修改Cookie

1

Response.Cookies("MyCookie")("Data") = myCookie

通过读取Request.Cookies集合的值得到Cookies的值

1

myCookie =Request.Cookies("MyCookie")("Data") ""

2、本人自编的一个函数,放到一个模块里直接调用即可:

Dim myCookie As String

Public Function LoginAnGetCookie(ByRef sUser As String, ByRef sPass As String) As String()

On Error Resume Next

REM 登录网站并获得cookie

Dim url As String = "网站地址"

Dim postData As String = "sUser=" sUser "sPass=" sPass '假设传值的是这样的,实际中可用Fiddler抓取。

Dim wc As System.Net.WebClient = New System.Net.WebClient

Rem wc.Headers.Add("Cookie", myCookie) '可讲第一次获得的Cookie赋值给myCookie,然后在此处传值给网站即可

wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

wc.Headers.Add("UserAgent", "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10)")

Dim sHtml As String = wc.UploadString(url, "POST", postData)

Dim arrAllKeys() As String = wc.ResponseHeaders.AllKeys

Dim sCookies As String = "Set-Cookie"

For i As Integer = 0 To arrAllKeys.Length - 1

If arrAllKeys(i).Equals("Set-Cookie") Then sCookies = wc.ResponseHeaders.Get(i)

Next i

Return New String() {sHtml, sCookies} '返回二维数组,其中LoginAnGetCookie(0)是网页源代码,LoginAnGetCookie(1)是登录后的Cookie

End Function

VB.NET 如何获取网页中的数据

Public Function webCaptureContent(ByVal mWebsiteUrl As String, ByVal mWebsiteType As Boolean) As String

'启动一次具体的数据采集工作,返回采集到的HTML内容:要求必须输入带://的全地址数据

On Error Resume Next

Dim Str_WebContent As String = "请输入查找网站地址."

Dim wb As WebClient = New WebClient() '//创建一个WebClient实例

If mWebsiteUrl.IndexOf("://") 0 Then

'//获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。(可有可无)

wb.Credentials = CredentialCache.DefaultCredentials

'//从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)

Dim pagedata As Object = wb.DownloadData(mWebsiteUrl)

'//转换字符

If mWebsiteType Then

Str_WebContent = Encoding.Default.GetString(pagedata)

Else

Str_WebContent = Encoding.UTF8.GetString(pagedata)

End If

End If

Return Str_WebContent '提取出来新闻内容,删除Body前后的多余内容,同时补充上该 Body标记,形成完整的内容 Str_WebContent '

End Function

求VB.NET读取网页内容写法

Imports System.Net

Imports System.IO

Imports System.Text.RegularExpressions

Public Class Form1

Private Sub button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim stream As IO.Stream = WebRequest.Create(UrlAdress).GetResponse().GetResponseStream()

'注意urladress为你上面的网页地址。

Dim sr As StreamReader = New StreamReader(stream, System.Text.Encoding.UTF8)

Label1.Text = Regex.Match(sr.ReadToEnd, "回答采纳率").ToString

'sr。readtoend读取网页流到末尾,即使用正则表达式从网页流中提取“回答采纳率”,赋值给Label1.Text ‘没有则为空

sr.Dispose() '关闭流

End Sub'要提取什么东西用正则表达式最好

End Class

如何用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就是网页源代码


新闻标题:vb.net如何爬取网页 vbnet抓取网页数据
文章路径:http://azwzsj.com/article/doejspg.html