版权声明:本文为博主原创文章,欢迎转载分享但请注明出处及链接,否则将其追究法律责!
1 2 3 4 5 6 7 8 9 | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url); request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"; request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; //此处换上每集的网址 request.Referer = "http://www.tingchina.com/pingshu/1228/play_1228_0.htm”; request.Method = "GET"; |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | /// <summary> /// 抓取网页内容 /// </summary> /// <param name="Url">网址</param> /// <param name="myEncoding">编码方式</param> /// <param name="myEncoding">请求的网址</param> /// <returns></returns> public string GetHtml(string Url, Encoding myEncoding, string Referer) { string HtmlString = ""; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url); request.Timeout = 15 * 1000; request.KeepAlive = true; request.AllowWriteStreamBuffering = true; request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.MaximumResponseHeadersLength = -1; request.Referer = Referer; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"; request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; request.ContentType = "application/x-www-form-urlencoded"; request.Method = "GET"; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Stream resStream = response.GetResponseStream(); StreamReader sr = new StreamReader(resStream, myEncoding); HtmlString = sr.ReadToEnd(); } } catch { } return HtmlString; } /// <summary> /// 下载评书的后台线程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bw_Download_DoWork(object sender, DoWorkEventArgs e) { //图书下载的本地路径 string LocalPath = e.Argument.ToString(); //查询出所有未下载的剧集 var query = from m in this._list where m.Status != 1 orderby m.ID ascending select m; //并行循环 var loopResult = Parallel.ForEach( query, new ParallelOptions { MaxDegreeOfParallelism = 1 }, (sound,loopStatue) =>{ //抓取剧集的详细页内容 string Html = GetHtml(sound.Url, Encoding.GetEncoding("GB2312"), ""); if (Html != "") { //分析播放音频网页的相关数据 Match ms_Info = Regex.Match(Html, @"src=""/play/" + this._category + @"/flash.asp\?id=[\d]*&inum=[\d]*&flei=(?<Category>[\s\S]*?)&bookname=(?<BookName>[\s\S]*?)&filename=(?<FileName>[\s\S]*?).mp3&nexturl", RegexOptions.IgnoreCase | RegexOptions.Multiline); if (ms_Info.Success) { //获取评书的最终名称和演播者 string[] tmp = ms_Info.Groups["BookName"].Value.Split('_'); if (tmp.Length == 2) { sound.Title = tmp[0]; sound.Performer = tmp[1]; } else { sound.Title = ms_Info.Groups["BookName"].Value; sound.Performer = ms_Info.Groups["Category"].Value; } //播放mp3的网页地址 string PlayUrl = "http://www.tingchina.com" + ms_Info.Value.Replace(@"src=""", "").Replace(@"&nexturl", ""); //评书的实际播放页面实际是嵌在详细页中的一个frame框架中,所以需要继续抓取播放评书的页面。 Html = GetHtml(PlayUrl, Encoding.Default, sound.Url); if (Html != "") { //抓取下载MP3的地址 MatchCollection ms = Regex.Matches(Html, @"url\[[\d]{1}\]= ""http://t(?<Number>[\d]*).tingchina.com""", RegexOptions.IgnoreCase | RegexOptions.Multiline); //抓取下载MP3所需的Key Match ms_Down = Regex.Match(Html, @"key=(?<key>[\d\w_]*)"";", RegexOptions.IgnoreCase | RegexOptions.Multiline); if (ms.Count > 0 && ms_Down.Success) { //音频mp3下载地址 string DownUrl = string.Format("http://t{0}.tingchina.com/{1}/{2}/{3}/{4}.mp3?key={5}", ms[0].Groups["Number"].Value, this._category, ms_Info.Groups["Category"].Value, ms_Info.Groups["BookName"].Value, ms_Info.Groups["FileName"].Value, ms_Down.Groups["key"].ToString()); WebClient client = new WebClient(); client.Headers.Add("Accept", "*/*"); client.Headers.Add("Accept-Encoding", "gzip, deflate"); client.Headers.Add("Cache-Control", "no-cache"); client.Headers.Add("Host", "t" + ms[0].Groups["Number"].Value + ".tingchina.com"); client.Headers.Add("Cookie", "Hm_lvt_99c9da471c839d239f4f41b80b233115=1445870536,1446212277,1446474813"); client.Headers.Add("UserAgent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"); client.Headers.Add("Referer", "http://www.tingchina.com/play/newflashv4.swf"); client.Headers.Add("x-flash-version", "20,0,0,267"); try { //开始下载MP3 client.DownloadFile(DownUrl, LocalPath + (sound.ID + 1).ToString().PadLeft(3, '0') + @".mp3"); sound.Status = 1; } catch (Exception ex) { sound.Status = -1; sound.Error = "下载MP3失败,原因:" + ex.Message; } } else { sound.Status = -1; sound.Error = "解析评书播放页的代码失败。"; } } else { sound.Status = -1; sound.Error = "抓取播放页的Html代码失败。"; } } else { sound.Status = -1; sound.Error = "解析详细页的Html代码失败。"; } } else { sound.Status = -1; sound.Error = "抓取详细页的Html代码失败。"; } } ); } private void bw_Download_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (!this.IsDisposed) { var query = from m in this._list where m.Status != 1 orderby m.ID ascending select m; if (query.Count() > 0) { this.btn_Download.Text = "继续下载"; } else { this.btn_Download.Text = "下载完毕"; } } } private void btn_Download_Click(object sender, EventArgs e) { //本地保存的路径 string LocalPath = Path.Combine(this._outpath, this._title) + @"\"; if (!Directory.Exists(LocalPath)) { try { Directory.CreateDirectory(LocalPath); } catch (Exception ex) { MessageBox.Show("创建评书下载目录失败,原因:" + ex.Message); } } using (BackgroundWorker bw_Download = new BackgroundWorker()) { bw_Download.WorkerReportsProgress = true; // 设置可以通告进度 bw_Download.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_Download_RunWorkerCompleted); bw_Download.DoWork += new DoWorkEventHandler(bw_Download_DoWork); bw_Download.RunWorkerAsync(LocalPath); } } |
©Copyright 2015-2021, 新雨网 [service@newrain.cn]。京ICP备15047473号-1
[页面更新时间:2024/9/16 8:49:06 ,构建时间:0.0312418 "秒]