public static WeiboComment GetComment(string mid, CookieContainer cc) { try { string commenturl = "http://weibo.com/aj/v6/comment/small?ajwvr=6&act=list&mid=" + mid + "&uid="; string result = HttpHelper1.SendDataByGET(commenturl, ref cc); WeiboComment comment = Newtonsoft.Json.JsonConvert.DeserializeObject <WeiboComment>(result); return(comment); } catch (Exception ex) { return(null); } }
private void btn_test_Click(object sender, EventArgs e) { Mblog mblog = null; string mid = ""; string ss = ""; string url = ""; string colresult = ""; MblogData mbloglist = null; int isHave = 0; WeiboComment comment = null; string links = "";//多链接以,分割 int j = 1; int linknum = 0; bool isCoupon = false; string couponlinks = ""; string[] urls = File.ReadAllLines(("config/colweibo.txt")); string[] xiaohaos = File.ReadAllLines(("config/xiaohao.txt")); alimamacc = Alimama.Login(); InitWeiboAccounts(); weibocc = WeiboHandler.InitWeiboCookie(((DEWeiboAccount)DEWeiboAccounts[0]).Username, "config", 1); while (true) { foreach (string urltemp in urls) { #region 抓取微博列表 url = urltemp + "1"; ss = ""; colresult = HttpHelper1.GetHttpsHtml(url, "", ref ss); if (colresult.Contains("")) { colresult = colresult.Replace("page\":null", "page\":1"); } mbloglist = Newtonsoft.Json.JsonConvert.DeserializeObject <MblogData>(colresult); #endregion #region 遍历处理需评论微博 foreach (Card card in mbloglist.Cards) { if (card.Mblog == null) { continue; } mblog = card.Mblog; mid = card.Mblog.Id; isHave = Convert.ToInt32(SQLiteHelper.ExecuteScalar("select count(*) from mblog where id=" + mid)); if (isHave > 0) { continue;//如果数据已存在,则跳过 } //先查看评论是否包含链接 comment = WeiboHandler.GetComment(mid, weibocc); if (comment == null) { continue; } if (comment.Data.Html.Contains("图1:")) { string tbklinks_html = ""; linknum = j = 1; couponlinks = ""; HttpHelper1.GetStringInTwoKeyword(comment.Data.Html, ref tbklinks_html, "图1:", "<!-- 评论图片的处理 -->", 0); Hashtable tbklinks = new Hashtable(); Regex reg = new Regex(@"[a-zA-z]+://[^\s]*"); MatchCollection mc = reg.Matches(tbklinks_html); foreach (Match m in mc) { linknum++; string weiboshortlink = m.Value.Replace("\"", ""); if (!weiboshortlink.StartsWith("http://t.cn")) { continue; } links = links + weiboshortlink + ","; //转换淘客微博评论链接为自有ID优惠券链接 //获取代理IP //通过代理IP登录微博 //通过代理IP发布评论 string tbrealitem = ""; string result = Alimama.GetItemResultWithWeiboShortUrl(weiboshortlink, alimamacc, ref tbrealitem); if (tbrealitem.Contains("http://ai.taobao.com")) { int staresult = SQLiteHelper.ExecuteNonQuery("insert into mblog(id,Source,Text,CreateAt,TbkLinks)values(@id,@Source,@Text,@CreateAt,@TbkLinks)", new[] { mblog.Id, mblog.Source, mblog.Text, mblog.CreatedAt, "" }); break; } DEWeiboAccount deweiboaccount = WeiboHandler.GetOneAccount(); //string own_tbklink = Weibo.Common.Alimama.GetTbkLink(tbrealitem, deweiboaccount.Siteid, deweiboaccount.Adzoneid, alimamacc);//把淘宝客链接更换为自己链接 //if (own_tbklink == "" || own_tbklink == "链接不支持转化") //{ // int staresult = SQLiteHelper.ExecuteNonQuery("insert into mblog(id,Source,Text,CreateAt,TbkLinks)values(@id,@Source,@Text,@CreateAt,@TbkLinks)", new[] { // mblog.Id, // mblog.Source, // mblog.Text, // mblog.CreatedAt, // "" // }); // break;//没有淘宝客链接,当前内容不可用,直接退出循环 //} //else if (own_tbklink == "阿里妈妈登录失效") //{ // alimamacc = Alimama.Login(); // if (Alimama.TestLogin(alimamacc)) // ; // else // { // return; // } //} double maxRate = 0; string searchresult = Alimama.SearchItem(tbrealitem, alimamacc); bool isSuc = Alimama.ApplyCampaign(searchresult, tbrealitem, ref alimamacc, ref maxRate);//申请定向计划 //获取优惠券信息 if (!searchresult.Contains("couponInfo\":\"无")) { string couponshorturl = Alimama.GetCouponInfo(searchresult, tbrealitem, alimamacc); string couponweiboShortlink = WeiboHandler.GetWeiboShorturl(couponshorturl);//微博短地址 if (couponweiboShortlink != "") { couponlinks += "图" + j.ToString() + ":" + couponweiboShortlink + " "; } isCoupon = true; } //string weiboShortlink = WeiboHandler.GetWeiboShorturl(own_tbklink);//微博短地址 //own_tbklinks += "图" + j.ToString() + ":" + weiboShortlink + " "; j++; } } if (!couponlinks.Contains("图")) { continue; } //WebProxy proxy = DailiHelper.GetProxy(); //while (!DailiHelper.VerIP(proxy)) //{ // proxy = DailiHelper.GetProxy(); //} Random r = new Random(); string xiaohao = xiaohaos[r.Next(xiaohaos.Length)]; string xiaohaousername = xiaohao.Split(',')[0]; string xiaohaopassword = xiaohao.Split(',')[1]; string uid = ""; HttpHelper1.GetStringInTwoKeyword(url, ref uid, "uid=", "&", 0); string refer = "http://www.weibo.com/u/" + uid + "/home?wvr=5"; CookieContainer xiaohaocc = WeiboHandler.InitWeiboCookie(xiaohaousername); if (!WeiboHandler.TestLogin(xiaohaocc)) { xiaohaocc = WeiboHandler.Login(xiaohaousername, xiaohaopassword); } while (xiaohaocc == null) { //取新号 xiaohao = xiaohaos[r.Next(xiaohaos.Length)]; xiaohaousername = xiaohao.Split(',')[0]; xiaohaopassword = xiaohao.Split(',')[1]; //proxy = DailiHelper.GetProxy(); xiaohaocc = WeiboHandler.InitWeiboCookie(xiaohaousername); if (!WeiboHandler.TestLogin(xiaohaocc)) { xiaohaocc = WeiboHandler.Login(xiaohaousername, xiaohaopassword); } } couponlinks = couponlinks.Replace("图", ""); string commentresult = WeiboHandler.Comment(mblog.Id, uid, couponlinks, refer, xiaohaocc); string decode_commentresult = Regex.Unescape(commentresult); if (!decode_commentresult.Contains("抱歉") && !decode_commentresult.Contains("广告")) { if (!commentresult.Contains("{\"code\":\"100000\"")) { //评论失败之后,再次评论 commentresult = WeiboHandler.Comment(mid, uid, "地址 " + couponlinks, refer, weibocc); } if (!commentresult.Contains("{\"code\":\"100000\"")) { //第二次评论尝试 commentresult = WeiboHandler.Comment(mid, uid, "链接 " + couponlinks, refer, weibocc); } if (!commentresult.Contains("{\"code\":\"100000\"")) { //第三次评论尝试 commentresult = WeiboHandler.Comment(mid, uid, "购 " + couponlinks, refer, weibocc); } } if (commentresult.Contains("{\"code\":\"100000\"")) { int statu = SQLiteHelper.ExecuteNonQuery("insert into mblog(id,Source,Text,CreateAt,TbkLinks)values(@id,@Source,@Text,@CreateAt,@TbkLinks)", new[] { mblog.Id, mblog.Source, mblog.Text, mblog.CreatedAt, couponlinks });//选品库下载完成后,保存微博数据到数据库 } }//单个mblog处理结束 #endregion } Thread.Sleep(10 * 60 * 1000); } }
// 获取指定目标中的所有微博评论,如果指定了目标ID,则只获取指定目标ID的评论 public WeiboData getTotalComments( string url, string selfId, string friendId) { cc = cc0; WeiboData weiboData = new WeiboData(); List<string> commentEntities = getTotalCommentEntities(cc, url, startPage, endPage); // 得到微博评论入口 // 遍历入口得到所有评论 foreach (var entity in commentEntities) { Console.Write("[" + threadName + "]评论入口:" + entity + " "); int n = 0; int maxCommentPage = 0; for (int i = 1; i <= (maxCommentPage == 0 ? 2 : maxCommentPage); i++) { string content = ""; try { content = SpiderHelper.RequestHttpGetWithCookie(entity + "&page=" + i, cc); } catch (Exception e) { Console.Write(e.Message); continue; } // 查找评论的最大页码 if (maxCommentPage == 0) { Match regMaxCommentPage = Regex.Match(content, "/([0-9]+)页</div>"); if (regMaxCommentPage.Length > 0) { maxCommentPage = int.Parse(regMaxCommentPage.Groups[1].Value); } maxCommentPage = maxCommentPage > 10 ? 10 : maxCommentPage; // 评论超过 10 页,应该也是大V没错,惹不起 } // // 评论示例:<a href="/u/1990224584?st=9771">目标用户</a>:<span class="ctt">哈哈,眼熟吧</span> // .*? 这种写法是因为c#的正则表达式匹配默认是贪婪模式,默认匹配最多字符,用这个可以禁止贪婪模式 string regstr = "<a href=\"/u/([0-9]+).*?\">(.*?)</a>.*?<span class=\"ctt\">.*?</span>"; MatchCollection mc = Regex.Matches(content, regstr, RegexOptions.Compiled | RegexOptions.IgnoreCase); if (mc.Count == 0) { cc = (cc == cc0) ? cc1 : cc0; // COOKIE已经被检测到异常,换一个COOKIE继续 } foreach (Match m in mc) { WeiboComment comment = new WeiboComment(); comment.userid = m.Groups[1].Value; string s = m.Value.Replace("</span>", ""); int k = s.LastIndexOf('>'); comment.comment = s.Substring(k + 1, s.Length - k - 1); comment.username = m.Groups[2].Value; n++; if (friendId == null) { // 此时是获取博主自身的 weiboData.comments.Add(comment); if (!comment.userid.Equals(selfId)) { // 有新的好友 WeiboUser friend; weiboData.friends.TryGetValue(comment.userid, out friend); if (friend == null) { friend = new WeiboUser(); friend.userid = comment.userid; friend.url = "http://weibo.cn/u/" + friend.userid + "?page="; friend.username = comment.username; weiboData.friends.Add(friend.userid, friend); } friend.count++; } continue; } else { // 此时是获取博主好友的,就只保存博主和好友说过的 if (comment.userid.Equals(selfId)) // || comment.userid.Equals(friendId)) { weiboData.comments.Add(comment); } } } } Console.WriteLine(n + " 条评论"); } return weiboData; }