private void bt_load_Click(object sender, EventArgs e) { if (IsInterNetConnected()) { try { string realUlr = tx_url.Text; HtmlHelper.HtmlFromWebReq req = new HtmlHelper.HtmlFromWebReq( realUlr, delegate(byte[] data) { //string constructedString = System.Text.Encoding.UTF8.GetString(data); string constructedString = System.Text.Encoding.GetEncoding("GB2312").GetString(data); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(constructedString); HtmlAgilityPack.HtmlNode nodeTitle = doc.DocumentNode.SelectSingleNode("/html[1]/head[1]/title"); string bookAuthor=""; string abstraction = nodeTitle.InnerHtml; int posOfBookMark = abstraction.IndexOf("》"); //获取最后一个)符号的位置 int posOflastBrace = abstraction.IndexOf(")", posOfBookMark); int temp = posOflastBrace; while (temp != -1) { posOflastBrace = temp; temp = abstraction.IndexOf(")", temp+1); } if (posOflastBrace != -1) bookAuthor = abstraction.Substring(posOfBookMark + 2, posOflastBrace - posOfBookMark - 2); HtmlAgilityPack.HtmlNodeCollection _nodes = doc.DocumentNode.SelectNodes(@"//html[1]/head[1]/meta[@name='keywords']"); foreach (HtmlAgilityPack.HtmlNode no in _nodes) { //no.GetAttributeValue("content",""); string s = no.GetAttributeValue("content", ""); string[] bookinfo = s.Split(new char[] { ',' }); string bookname = bookinfo[0]; if (!String.IsNullOrEmpty(bookinfo[1])) bookAuthor = bookinfo[1]; string bookPublisher = bookinfo[2]; string bookISBN = bookinfo[3]; SetText( bookname + "\n" + bookAuthor + "\n" + bookPublisher + "\n" + bookISBN); } //获取书籍图片 HtmlAgilityPack.HtmlNode pBoxNode = doc.DocumentNode.SelectSingleNode(@"//html[1]/body[1]/div[@id='p-box']/div[3]/div[@id='product-intro']/div[@id='preview']/div[@id='spec-n1']"); if (pBoxNode != null) { HtmlAgilityPack.HtmlNode image = pBoxNode.SelectSingleNode(@"//img[1]"); if (image != null) { string imagesrc = image.GetAttributeValue("src", ""); using(WebClient wc = new WebClient()) { picBox.Load("http:"+imagesrc); } } } }); req.BeginCreateHtml(); } catch (Exception ex) { throw ex; } } else MessageBox.Show("没有互联网!"); }
private void button1_Click(object sender, EventArgs e) { if (IsInterNetConnected()) { try { string realUlr = tx_url.Text; HtmlHelper.HtmlFromWebReq req = new HtmlHelper.HtmlFromWebReq( realUlr, delegate(byte[] data) { //string constructedString = System.Text.Encoding.UTF8.GetString(data); string constructedString = System.Text.Encoding.UTF8.GetString(data); SetText(constructedString); //Object o = JsonConvert.DeserializeObject(constructedString); //Console.WriteLine(o.ToString()); JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic dy = jss.Deserialize(constructedString, typeof(object)) as dynamic; }); req.BeginCreateHtml(); } catch (Exception ex) { throw ex; } } else MessageBox.Show("没有互联网!"); }
private void bt_syncRecent_Click(object sender, EventArgs e) { if (CommonHelper.IsInterNetConnected()) { try { string ulrPrefix = @"http://kaijiang.zhcw.com/zhcw/html/ssq/list_"; string ulrSufix = @".html"; string realUlr = ulrPrefix + "1" + ulrSufix; HtmlHelper.HtmlFromWebReq req = new HtmlHelper.HtmlFromWebReq( realUlr, delegate(byte[] data) { string constructedString = System.Text.Encoding.UTF8.GetString(data); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(constructedString); //HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[1]/tr[3]/td[3]"); HtmlAgilityPack.HtmlNodeCollection _nodes = doc.DocumentNode.SelectNodes("/html[1]/body[1]/table[1]/tr[position()>2]"); foreach (HtmlAgilityPack.HtmlNode no in _nodes) { string s = no.InnerText; s = s.Trim().Replace(" ", "");//.Replace('\n',' ').Replace('\r',' ');//Replace(" ","|"); string[] lines = s.Split(new char[] { '\r', '\n' }); List <string> _infos = new List <string>(); foreach (string ss in lines) { if (string.IsNullOrEmpty(ss)) { continue; } _infos.Add(ss); } if (!(_infos.Count == 12 || _infos.Count == 13)) { continue; } DoubleBoll temp = new DoubleBoll(); string riqi = _infos[0]; string qishu = _infos[1]; string r1 = _infos[2]; string r2 = _infos[3]; string r3 = _infos[4]; string r4 = _infos[5]; string r5 = _infos[6]; string r6 = _infos[7]; string b1 = _infos[8]; string allmoney = _infos[9]; string fno = "", fpro = "", sno = ""; if (_infos.Count == 13) { fno = _infos[10]; fpro = _infos[11]; sno = _infos[12]; } else if (_infos.Count == 12) { fno = _infos[10]; sno = _infos[11]; } string[] ymd = riqi.Split(new char[] { '-' }); DateTime opendate = new DateTime(Convert.ToInt32(ymd[0]), Convert.ToInt32(ymd[1]), Convert.ToInt32(ymd[2])); temp.OpenDate = opendate; temp.Red1 = Convert.ToInt32(r1); temp.Red2 = Convert.ToInt32(r2); temp.Red3 = Convert.ToInt32(r3); temp.Red4 = Convert.ToInt32(r4); temp.Red5 = Convert.ToInt32(r5); temp.Red6 = Convert.ToInt32(r6); temp.Blue = Convert.ToInt32(b1); temp.SaleTotal = Convert.ToInt32(allmoney.Replace(",", "")); temp.TouJiangZhuShu = Convert.ToInt32(fno); temp.ErJiangZhuShu = Convert.ToInt32(sno); temp.FirstProvince = fpro; temp.QiShu = qishu; if (!DBHelper.SaveToDB(_db, temp, true)) { DBHelper.UpdateFromDB(_db, temp); } } //string temp = node.InnerText; MessageBox.Show("完成"); }); req.BeginCreateHtml(); } catch (Exception ex) { LOGGER.Error("**************导入数据错误***********", ex); } } else { MessageBox.Show("没有互联网!"); } }