private CookieCollection SplitCookie(string str) { str = str.Replace(" ", ""); str = str.Replace("\n", ""); str = str.Replace(",", "%2C"); string[] everyCookie = str.Split(';'); CookieCollection cookie = new CookieCollection(); foreach (string tmp in everyCookie) { try { cookie.Add(new Cookie(StrongString.GetLeft(tmp, "="), StrongString.GetRight(tmp, "="))); }catch (CookieException e) { throw e; } } return(cookie); }
private void GetWeiBoData() { //设置url和contentType //string url = "https://s.weibo.com/weibo?q=%E5%88%86%E6%89%8B&page=1"; string url = "https://s.weibo.com/weibo?q=%E5%88%86%E6%89%8B&nodup=1"; string contentType = "text/html; charset=UTF-8"; //将用户的cookie放入容器 CookieCollection cookie; try { cookie = SplitCookie(TxtCookie.Text); } catch { MessageBox.Show("cookie输入错误,请重新检查!"); return; } //post访问 byte[] webData = HttpWebClient.Post(url, new byte[0], contentType, "", ref cookie); //返回数据编码 string strData = Encoding.UTF8.GetString(webData); List <string> strList = StrongString.BetweenArr(strData, "<p class=\"txt\" node-type=\"feed_list_content\"", "</p>"); //对数据去重 Deduplication(strList); //放入数据源 //datas.Clear(); for (int i = 0; i < strList.Count; i++) { WeiboData tmp = new WeiboData(); tmp.Time = DateTime.Now.ToString(); //格式化内容(防止出现太多的html代码 tmp.Content = StrongString.GetRight(strList[i], ">"); tmp.Content = tmp.Content.Replace("<em class=\"s-color-red\">", ""); tmp.Content = tmp.Content.Replace("</em>", ""); tmp.Content = tmp.Content.Replace("\n", ""); //去重检测 if (datas.Count == 0) { datas.Insert(0, tmp); } else { for (int j = 0; j < datas.Count; j++) { if (datas[j].Content == tmp.Content) { break; } else if (j == datas.Count - 1) { datas.Insert(0, tmp); break; } } } } }