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"; |
1
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); } } |