您的位置:首页 > 编程语言 > ASP

asp.net(VB)中使用正则获取图片地址并进行下载.

2011-09-19 14:47 806 查看
使用时需要导入.net命名空间,本例对复制到文本框中的网页源代码获取其中图片地址,并下载,你也可以根据需要把图片在你的页面上进行显示.

Imports System.Net

Partial Class Query_QImage

    Inherits System.Web.UI.Page

    Function Htmlimg(ByVal str As String) As String

        Dim Himg As String = ""

        Dim rg As Regex

        rg = New Regex("<img.+?>")

        Dim mm = rg.Matches(str)

        For Each Match1 In mm

            Himg = Himg & Getimgs(Match1.value)

        Next

        Return Himg

    End Function

Function Getimgs(ByVal str As String) As String

        Dim Gimgs As String = ""

        Dim rg As Regex

        rg = New Regex("http://([^']+(?:jpg""|gif""|png""|bmp""|jpeg""))") '("http://.+?.jpg""")

        Dim mm = rg.Matches(str)

        For Each Match1 In mm

            Gimgs = Gimgs & "|" & Left(Match1.Value, Len(Match1.Value) - 1)

        Next

        Return Gimgs

    End Function

'对代码中的图片进 行下载,保存到d盘,指定的路径一定要存在.

 Protected Sub btn_down_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_down.Click

        Dim wc As New WebClient

        Dim imgs As String '所有图片的地址

        'Dim imgdata As Byte() ' imgdata = wc.DownloadData(url)  'Response.BinaryWrite(imgdata)

        Dim path As String = "D:\Test\"

        Dim Fname As String = "" '图片的名称

        Dim url As String = "" '图片的url地址

        Dim img() As String

        Dim sx() As String

        Dim sss As String = ""

        imgs = Htmlimg(txt_lr.Text)

        img = Split(imgs, "|")

        txt_lr.Text = imgs

        For Each ss In img

            url = ss

            sx = Split(ss, "/")

            Fname = sx(sx.Length - 1)

            If Fname = "" Then

            Else

                sss += path & Fname

                wc.DownloadFile(url, path & Fname)

            End If

        Next

        txt_lr.Text = sss

 End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net vb string regex url each
相关文章推荐