protected override void OnDoWork(System.ComponentModel.DoWorkEventArgs e) { this.WorkerReportsProgress = true; var listMI = GetMovie(); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); listResult = new List <MovieDetail>(); var total = (double)listMI.Count; int n = 1; foreach (MovieItem item in listMI) { if (this.CancellationPending) { e.Cancel = true; return; } try { var o = (this.wc as MyWebClient).GetHTML(new Uri(item.DetailURL)); doc.LoadHtml(o); } catch (WebException err) { continue; } n++; MovieDetail md = new MovieDetail(); int percent = (int)((n / total) * 100); if (percent > 100) { percent = 100; } ReportProgress(percent, "第" + n.ToString() + "个/共" + total + ":" + item.Title); md.DetailURL = item.DetailURL; md.Tag = item.Tag; try { md.Title = item.Title; //md.Title = doc.DocumentNode.SelectNodes("//h1[@class='kw']")[0].InnerText.Trim(); } catch (Exception) { md.Title = item.Title; } try { md.MainActor = item.MainActor; } catch (Exception) { md.MainActor = item.MainActor; } try { int pCount = doc.DocumentNode.SelectNodes("//div[@class='txt']/p").Count; int localP = 0; for (int jj = 1; jj < pCount + 1; jj++) { md.Location = doc.DocumentNode.SelectNodes("//div[@class='txt']/p[" + jj.ToString() + "]")[0].InnerText; if (md.Location.IndexOf("地区") != -1) { localP = jj; break; } } md.Location = doc.DocumentNode.SelectNodes("//div[@class='txt']/p[" + localP + "]")[0].InnerText.Replace("地区:", "").Replace("\r\t", "").Trim(); } catch (Exception) { md.Location = ""; } try { md.MType = doc.DocumentNode.SelectNodes("//p[@class='v_tags']")[0].InnerText.Replace("类型:", "").Replace("\r\n", "").Replace("\t", "").Trim(); } catch (Exception) { md.MType = ""; } try { int pCount = doc.DocumentNode.SelectNodes("//div[@class='txt']/p").Count; //if (pCount == 7) //{ // md.YearAbout = doc.DocumentNode.SelectNodes("//div[@id='panelAlbumDetail']/div[3]/p[5]")[0].InnerText.Replace("年代:", "").Replace("\r\n", "").Trim(); //} //else if (pCount == 6) //{ // md.YearAbout = doc.DocumentNode.SelectNodes("//div[@id='panelAlbumDetail']/div[3]/p[4]")[0].InnerText.Replace("年代:", "").Replace("\r\n", "").Trim(); //} int yearP = 0; for (int ii = 1; ii < pCount + 1; ii++) { md.YearAbout = doc.DocumentNode.SelectNodes("//div[@id='panelAlbumDetail']/div[3]/p[" + ii.ToString() + "]")[0].InnerText; if (md.YearAbout.IndexOf("年代:") != -1) { yearP = ii; break; } } md.YearAbout = doc.DocumentNode.SelectNodes("//div[@id='panelAlbumDetail']/div[3]/p[" + yearP.ToString() + "]")[0].InnerText.Replace("年代:", "").Replace("\r\n", "").Trim(); //else if (pCount==5) //{ //md.YearAbout = doc.DocumentNode.SelectNodes("//div[@id='panelAlbumDetail']/div[3]/p[3]")[0].InnerText.Replace("年代:", "").Replace("\r\n", "").Trim(); //} } catch (Exception) { md.YearAbout = ""; } try { //int cc = doc.DocumentNode.SelectNodes("//span[@class='pub']").Count; //if (cc == 2) //{ // md.YoukuPublishDate = ""; //} //if (cc == 3) //{ // md.YoukuPublishDate = doc.DocumentNode.SelectNodes("//span[@class='pub']")[2].InnerText.Replace("上映:", "").Replace("\t", "").Trim(); //} md.YoukuPublishDate = ""; } catch (Exception) { md.YoukuPublishDate = ""; } try { md.Director = doc.DocumentNode.SelectNodes("//div[@id='panelAlbumDetail']/div[3]/p[1]")[0].InnerText.Replace("导演:", "").Replace("\r\n", " ").Replace("\t", "").Trim(); } catch (Exception) { md.Director = ""; } try { md.CoverFile = item.CoverFile; } catch (Exception) { md.CoverFile = ""; } try { //http://www.tudou.com/crp/itemSum.action?jsoncallback=page_play_model_itemSumModel__find&app=3&showArea=true&iabcdefg=130442880&uabcdefg=0&juabcdefg=01984snkkf2rrk md.TotalPlayed = item.Tag.ToString(); } catch (Exception) { md.TotalPlayed = ""; } try { md.UpdateInfo = item.UpdateInfo; } catch (Exception) { md.UpdateInfo = item.UpdateInfo; } listResult.Add(md); //调试运行。正常运行请注释掉 //break; } e.Result = listResult; }
protected override void OnDoWork(System.ComponentModel.DoWorkEventArgs e) { this.WorkerReportsProgress = true; var listMovieItem = GetMovie(); HtmlAgilityPack.HtmlDocument doc = new HtmlDocument(); listResult = new List <MovieDetail>(); var total = (double)listMovieItem.Count; int n = 1; foreach (MovieItem item in listMovieItem) { if (this.CancellationPending) { e.Cancel = true; return; } try { var o = (this.wc as MyWebClient).GetHTML(new Uri(item.DetailURL)); doc.LoadHtml(o); } catch (WebException err) { continue; } n++; MovieDetail md = new MovieDetail(); int percent = (int)((n / total) * 100); if (percent > 100) { percent = 100; } ReportProgress(percent, "第" + n.ToString() + "个/共" + total + ":" + item.Title); md.DetailURL = item.DetailURL; md.Tag = item.Tag; try { //md.Title = doc.DocumentNode.SelectNodes("//div[@id='title']")[0].InnerText.Replace(" ", "").Replace("电影:", "").Replace("\n", "").Replace("\t", "").Trim(); md.Title = item.Title; } catch (Exception) { md.Title = item.Title; } try { md.MainActor = doc.DocumentNode.SelectNodes("//span[@class='actor']")[0].InnerText.Replace("主演:", "").Replace("\t", "").Trim(); } catch (Exception) { md.MainActor = item.MainActor; } try { md.Location = doc.DocumentNode.SelectNodes("//span[@class='area']")[0].InnerText.Replace("地区:", "").Replace("\t", "").Trim(); } catch (Exception) { md.Location = ""; } try { md.MType = doc.DocumentNode.SelectNodes("//span[@class='type']")[1].InnerText.Replace("类型:", "").Replace("\t", "").Trim(); } catch (Exception) { md.MType = ""; } try { md.YearAbout = doc.DocumentNode.SelectNodes("//span[@class='pub']")[1].InnerText.Replace("上映:", "").Replace("\t", "").Trim(); } catch (Exception) { md.YearAbout = ""; } try { int cc = doc.DocumentNode.SelectNodes("//span[@class='pub']").Count; if (cc == 2) { md.YoukuPublishDate = ""; } if (cc == 3) { md.YoukuPublishDate = doc.DocumentNode.SelectNodes("//span[@class='pub']")[2].InnerText.Replace("上映:", "").Replace("\t", "").Trim(); } } catch (Exception) { md.YoukuPublishDate = ""; } try { md.Director = doc.DocumentNode.SelectNodes("//span[@class='director']")[0].InnerText.Replace("导演:", "").Replace("\t", "").Trim(); } catch (Exception) { md.Director = ""; } try { md.CoverFile = doc.DocumentNode.SelectNodes("//li[@class='thumb']")[0].FirstChild.Attributes["src"].Value.ToString().Trim(); } catch (Exception) { md.CoverFile = ""; } try { md.TotalPlayed = doc.DocumentNode.SelectNodes("//span[@class='play']")[0].InnerText.Replace("总播放:", "").Replace(" ", "").Trim(); } catch (Exception) { md.TotalPlayed = ""; } try { var tag = doc.DocumentNode.SelectNodes("//div[@class='basenotice']")[0].InnerText; md.Tag = tag.Substring(0, tag.IndexOf("@")).Replace("\t", "").Replace("\n", "").Trim(); } catch (Exception) { md.Tag = ""; } listResult.Add(md); } e.Result = listResult; }
void mgb_DoSingleWork(object sender, DoWorkEventArgs e) { //throw new NotImplementedException(); MyWebClient mmwc = new MyWebClient(); string mhtml = mmwc.GetHTML(new Uri((e.Argument as MovieItem).DetailURL)); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(mhtml); var nodes = doc.DocumentNode.SelectNodes("//div[@class='play-detail-text-area clearfix']"); string title, lo, tp, direc, actors; try { title = doc.DocumentNode.SelectSingleNode("//div[@class='play-detail-text clearfix']/h2").InnerText; } catch (Exception) { title = ""; } try { lo = nodes[0].SelectNodes("p[@class='play-detail-area-l']")[0].InnerText; } catch (Exception) { lo = ""; } try { tp = nodes[0].SelectNodes("p[@class='play-detail-area-r']")[0].InnerText; } catch (Exception) { tp = ""; } try { direc = nodes[1].SelectNodes("p[@class='play-detail-area-l']")[0].InnerText; } catch (Exception) { direc = ""; } try { actors = nodes[1].SelectNodes("p[@class='play-detail-area-r']")[0].InnerText; } catch (Exception) { actors = ""; } MovieDetail md = new MovieDetail(); md.Location = lo.Replace("地区:", ""); if (reg.IsMatch(md.Location)) { md.YearAbout = reg.Match(md.Location).ToString(); md.Location = reg.Replace(md.Location, "").Trim(); } else { md.YearAbout = ""; } md.MType = tp.Replace("类型:", ""); md.Title = title; md.Director = direc.Replace("导演:", ""); md.MainActor = actors.Replace("主演:", ""); md.CoverFile = ""; md.DetailURL = (e.Argument as MovieItem).DetailURL; md.Tag = ""; md.TotalPlayed = "0"; md.UpdateInfo = ""; md.YoukuPublishDate = ""; e.Result = md; }