• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
皇图霸业谈笑间
更高、更快、更强
博客园    首页    新随笔    联系   管理    订阅  订阅
用微软控件执行网页中的脚本:msscript.ocx

用到的控件有: msscript.ocx   ,  msscrchs.dll   。 引用其中的  msscript.ocx 在项目中即可;

 示例代码如下 :

 

代码


        
#region 搜索歌曲的下载歌曲
        
/// <summary>
        
/// 从百度的试听页面获取歌曲的下载地址
        
/// </summary>
        
/// <param name="url"></param>
        
/// <returns></returns>
        public static string DecodeMP3Url(string url)
        {
            
string content = GetReponseText(url, Encoding.GetEncoding("gb2312"), string.Empty);

            Regex objRegex 
= new Regex("var sertim =(?<sertimvalue>[\\S ]*?), statim =(?<statimvalue>[\\S ]*?);",
                   RegexOptions.IgnoreCase 
| RegexOptions.Compiled);
            Match objMatch 
= objRegex.Match(content);

            
if (objMatch.Success)
            {
                
//获取变量定义
                string vardefine = objMatch.Value;

                objRegex 
= new Regex("function decode\\(url\\){(?<body>[\\S\\s]*?)return decurl;(?<g>[\\s]*?)}",
                   RegexOptions.IgnoreCase 
| RegexOptions.Compiled);
                objMatch 
= objRegex.Match(content);
                
if (objMatch.Success)
                {
                    
//获取加密的函数体
                    string functiondefine = objMatch.Value;

                    
//获取加密后的下载地址列表
                    objRegex = new Regex("\"(?<t>[\\S]{4})://(?<g>[\\S]*?)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    objMatch = objRegex.Match(content);
                    List
<string> pathlist = new List<string>();
                    
while (objMatch.Success)
                    {
                        
string v = objMatch.Value.Substring(1, objMatch.Value.Length - 2);
                        
if (!v.Contains("...")
                            
&& !pathlist.Contains(v)
                            
&& !v.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
                        {
                            pathlist.Add(v);
                        }
                        objMatch 
= objMatch.NextMatch();
                    }

                    ScriptControl sc 
= new ScriptControl();
                    sc.Language 
= "JavaScript";
                    sc.AllowUI 
= false;
                    sc.AddCode(vardefine);
                    sc.AddCode(functiondefine);

                    
foreach (string path in pathlist)
                    {
                        
//解密
                        string realurl = sc.Eval(string.Format("decode('{0}')", path)).ToString();
                        
if (CheckMusicUrl(realurl))
                        {
                            
return realurl;
                        }
                    }
                }
            }

            
return string.Empty;
        }
        
static string GetReponseText(string sUrl, Encoding encode, string sAgent)
        {
            HttpWebRequest request 
= HttpWebRequest.Create(sUrl) as HttpWebRequest;
            
//request.AllowAutoRedirect = false;
            Uri uri = new Uri(sUrl);
            request.Referer 
= "http://" + uri.Host;
            request.UserAgent 
= sAgent;

            
string result;
            WebResponse response 
= request.GetResponse();
            
using (Stream stream = response.GetResponseStream())
            {
                
using (StreamReader reader = new StreamReader(stream, encode))
                {
                    result 
= reader.ReadToEnd();
                }
            }
            response.Close();
            
return result;
        }


上面的代码是采集百度的MP3音乐,因为百度的音乐URL是用JS脚本构造出来的

所以我就直接用他的脚本,在C#后台进行URL构造,然后抓取地址内容。

 

 

 

 

posted on 2010-02-22 11:59  布颜书  阅读(1783)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3