第1:
用WebBrowser控件作为浏览器,浏览一些网页,他们地址形式多种多样,例如:http://www.abc.com/temp/bbs/index.asp 或者 http://adfadf.com 或者 http://www.xx.net/a/default.htm
我能通过vb根据当前浏览的网页获得这个网页地址的前缀吗?
例如我访问:http://www.abc.com/temp/bbs/index.asp 则希望获得:http://www.abc.com/temp/bbs/
例如我访问:http://adfadf.com 则希望获得:http://adfadf.com
例如我访问:http://www.xx.net/a/default.htm 则希望获得:http://www.xx.net/a/
请教代码如何写?第2:
如果网页中有些这样的JavaScript或者VbScript变量,例如:
aa= 34242
bb = "3424sdfasf"
cc =True我只会使用笨方法通过字符串查找定位后再截取出值,但是我觉得这样做太低效率了,有否高效的方式截取出:34242、"3424sdfasf"、True
需要注意哦,网页脚本里这些变量的“=”号之间的空格是不定量的。有时候“=”号都会有空格,有时候“=”号两边都没,有时候“=”号只有一边有空格。
请教代码如何写?

解决方案 »

  1.   

    第一问:Dim strWebsitePrefix As String
    Const strWebsite = "http://adfadf.com"strWebsitePrefix = IIf(InStr(8, strWebsite, "/"), _
    Left(strWebsite, InStrRev(strWebsite, "/")), strWebsite)
      

  2.   

    第二问:
    搜索有很多方法,不过效率高的算法我目前没有,顶着看高人指点了,取值的算法试试下面的Dim strValue As String
    Const strVariable = "aa= 34242"strValue = LTrim(Mid(strVariable, InStr(strVariable, "=") + 1, Len(strVariable)))
      

  3.   

    首先谢谢,也许朋友没看仔细我的帖子,我那个是举例,而并非事前知道指定的网址,用WebBrowser控件是可以任意浏览网页的,也就说前缀是任意的,那么如何提取呢?
      

  4.   

    Sorry,我的笨办法不可行,因为有些网址后面的明参是可以带有网址的,例如:http://www.abc.com/c.asp?a=54&pwline=http://ccc.net
      

  5.   

    第一,获得网站名没问题,2个//后第一个/之间。你说的"前缀"概念不清。
    第二,用笨办法也行。不知道有没有API可以直接获取JavaScript或者VbScript变量?
      

  6.   

    第一个问题我发了另一个帖子自己找朋友帮忙用正则方式解决了。Private Sub Command1_Click()
        MsgBox getURL(WebBrowser1.LocationURL)
    End SubFunction getURL(sURL)
        Set re = New RegExp
        re.IgnoreCase = True
        re.Global = True
        re.Pattern = "^((http|https|ftp):\/\/[^\/?]+(.(?=(($)|(\/))))(\/([^?]+\/)*)?)"
        Set oMatches = re.Execute(sURL)
        If oMatches.Count > 0 Then getURL = oMatches(0)
    End Function现在剩下第二个问题
    “有API可以直接获取JavaScript或者VbScript变量”,如果有这样的api不错啊,这样就可以高效提取网页脚本语言的变量值了。
    第二个问题不晓得正则能搞定不?
      

  7.   

    第二个问题自己找朋友问到比较折中的解决办法了,共享下,之后结贴:
    Private Sub Command1_Click()
    Dim jsstr
    jsstr = "<SCRIPT language=JavaScript>" & vbCrLf & _
            "var j,u,p,r,w,s" & vbCrLf & _
            "j='""=dhf""thfgh564""ghdf==""'" & vbCrLf & _
            "u= true" & vbCrLf & _
            "p = 1.8766-e23;" & vbCrLf & _
            "r =""$^$%^$%55===^$%""" & vbCrLf & _
            "w=   null" & vbCrLf & _
            "s =543" & vbCrLf & _
            "</script>"
        GetValue (jsstr)
    End SubFunction GetValue(mStr) As Boolean
        Dim re
        Dim oMatches
        Set re = New RegExp
        re.IgnoreCase = True
        re.Global = True
        re.MultiLine = True
        re.Pattern = "\s[juprws]\s*=\s*([""']?)(.+?)(\1)(?=(\s|;))"
        Set oMatches = re.Execute(mStr)
        If oMatches.Count > 0 Then
            GetValue = True
            For Each matche In oMatches
                MsgBox matche.SubMatches(1)
            Next
        Else
            GetValue = False
        End If
    End Function
      

  8.   


    Private Sub Command1_Click()
    Dim jsstr
    jsstr = "<SCRIPT language=JavaScript>" & vbCrLf & _
            "var j,u,p,r,w,s" & vbCrLf & _
            "j='""=dhf""thfgh564""ghdf==""'" & vbCrLf & _
            "u= true" & vbCrLf & _
            "p = 1.8766-e23;" & vbCrLf & _
            "r =""$^$%^$%55===^$%""" & vbCrLf & _
            "w=   null" & vbCrLf & _
            "s =543" & vbCrLf & _
            "</script>"
            RichTextBox1.Text = jsstr
        GetValue (RichTextBox1.Text)
    End SubFunction GetValue(mStr) As Boolean
        Dim re
        Dim oMatches
        Set re = New RegExp
        re.IgnoreCase = True
        re.Global = True
        re.MultiLine = True
        re.Pattern = "\s[juprws]\s*=\s*([""']?)(.+?)(\1)(?=(\s|;))"
        Set oMatches = re.Execute(mStr)
        If oMatches.Count > 0 Then
            GetValue = True
            For Each matche In oMatches
                MsgBox matche.SubMatches(1)
            Next
        Else
            GetValue = False
        End If
    End Function