
正文
第一篇随笔:用VB.NET搞点简单事情(1)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
网络上能搜索到的爬虫文章大多是用python做的,也有少部分是C#做的(小声:所以用VB.NET也可以做爬虫.本文写的是第一步:获取网页)
使用代码前先imports以下内容
Imports System.IO, System.IO.Compression, System.Text, System.Net
写程序前先开浏览器(我用的Chrome),随便上个网页,F12看下header,粘下来useragent备用,也可以粘下accept,cookie等(在本文中用不到
用httpwebrequest建立请求,用httpwebresponse得到响应体.然后考虑下压缩的问题(imports System.IO.Compression就是解决这个的)
最后得到真正的返回流,streamreader读取之,然后网页的http代码就搞下来了.用这种方法可以搞定编码为UTF-8的网页对于编码是GB2312或GBK的需有改动:使用streamreader时第二个参数改为Encoding.GetEncoding("gbk")
下面是代码:
Public Function GetHttpContent(url As String) As String
Try
Dim req As HttpWebRequest = HttpWebRequest.CreateHttp(url), resp As HttpWebResponse, sol$
With req
.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
.Accept = "*/*"
.Method = "GET"
.Timeout =
.Headers.Add("accept-encoding", " gzip, deflate")
End With
resp = req.GetResponse
Select Case resp.ContentEncoding.ToLower
Case "gzip"
Using z As New GZipStream(resp.GetResponseStream, CompressionMode.Decompress)
Using sr As New StreamReader(z, Encoding.UTF8)
sol = sr.ReadToEnd
End Using
End Using
Exit Select
Case "deflate"
Using z As New DeflateStream(resp.GetResponseStream, CompressionMode.Decompress)
Using sr As New StreamReader(z, Encoding.UTF8)
sol = sr.ReadToEnd
End Using
End Using
Exit Select
Case Else
Using sr As New StreamReader(resp.GetResponseStream, Encoding.UTF8)
sol = sr.ReadToEnd
End Using
Exit Select
End Select
Return sol
Catch ex As Exception
Return ""
End Try
End Function
(本人水平有限,代码有不完善的地方欢迎指出






