百度音乐解析器


        Sub getUrl(ByVal str As String)
        Dim myRequest As HttpWebRequest = Nothing
        Dim myHttpResponse As HttpWebResponse = Nothing
        Dim doubanurl As String = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="
        doubanurl = doubanurl + str + "$$$$$$"
        myRequest = WebRequest.Create(doubanurl)
        myHttpResponse = myRequest.GetResponse()
        Dim reader As StreamReader = New StreamReader(myHttpResponse.GetResponseStream())
        Dim xmldetail As String = reader.ReadToEnd()
        reader.Close()
        myHttpResponse.Close()
        '以下为歌曲搜索部分
        Dim all As String = xmldetail
        Label5.Text = "正在为您搜索,请稍后...."
        Dim countStart As Integer = InStr(all, "<count>") + Len("<count>")
        Dim countEnd As Integer = InStr(all, "</count>")
        count = Val(Mid(all, countStart, countEnd - countEnd + 1)) '获取返回歌曲结果的数目
        '开始音乐搜索
        Dim encodeStart As Integer
        Dim encodeEnd As Integer
        Dim decodeStart As Integer
        Dim decodeEnd As Integer
        Dim url1 As String
        Dim url2 As String
        encodeStart = 1 : encodeEnd = 1 : decodeStart = 1 : decodeEnd = 1
        Dim i As Integer = 0 '设置记录变量i,用于统计数据
        '反复读取寻找歌曲链接
        Do
            Dim s1 As Integer = encodeStart
            Dim e1 As Integer = encodeEnd
            Dim s2 As Integer = decodeStart
            Dim e2 As Integer = decodeEnd
            encodeStart = InStr(s1, all, "<encode><![CDATA[") + Len("<encode><![CDATA[")
            encodeEnd = InStr(e1 + Len("]]></encode>"), all, "]]></encode>")
            url1 = Mid(all, encodeStart, encodeEnd - encodeStart + 1)
            decodeStart = InStr(s2, all, "<decode><![CDATA[") + Len("<decode><![CDATA[")
            decodeEnd = InStr(e2 + Len("]]></decode>"), all, "]]></decode>")
            url2 = Mid(all, decodeStart, decodeEnd - decodeStart + 1)
            ListBox1.Items.Add(Trim(url1) & Trim(url2))
            i = i + 1
        Loop Until i > count - 1
        '音乐链接搜索结束   //这里的情况比较复杂,暂时搞不定
        'Dim lrcStart As Integer
        'Dim lrcEnd As Integer
        'Dim lrc1 As Integer
        'Dim lrc2 As Integer
        'lrcStart = 1 : lrcEnd = 1 : i = 0
        ''以下为歌词搜索部分(
        'Do
        '    Dim l1 As Integer = lrcStart
        '    Dim l2 As Integer = lrcEnd
        '    lrcStart = InStr(l1, all, "<lrcid>") + Len("<lrcid>")
        '    lrcEnd = InStr(l2 + Len("</lrcid>"), all, "</lrcid>")
        '    lrc1 = Val(Mid(all, lrcStart, lrcEnd - lrcStart + 1)) '转化为数字
        '    lrc2 = Int(lrc1 / 100)
        '    ListBox1.Items.Add("http://box.zhangmen.baidu.com/bdlrc/" & str(lrc2) & "/" & lrc1 & ".lrc")
        '    i = i + 1
        'Loop Until i > count - 1
        '歌词搜索部分()
        Label5.Text = "已为您找到" & count & "首相关音乐"
        End Sub 
 
       程序可以用xml处理,但是非法字符的问题没法解决,所以用了这样的方法。
       考虑过用C#来写,但是最后循环查找实在写不好,只能作罢了。
       一个编程菜鸟
       2012年12月29日

你可能感兴趣的:(音乐)