微信获取好友信息 - 微信导出好友数据分析

时间: 作者: 点击量:

 

这个Demo是利用HttpWebRequest和HttpWebResponse来爬取微信好友,讲信息存入数据库,并对信息进行分析,用图标画出来。

如何获得好友信息

 首先前往https://wx.qq.com/登录自己的微信账号,打开浏览器的开发者控制台

微信给后台发送了几十个请求,我翻遍了所有请求,终于找到了想要的url

借助浏览器我们可以获取这个请求的信息

我们可以把这个数据交给程序让程序帮我们取请求,并把数据保存下来

前期准备新建一个名为WeChatAnalyse的项目

借助nuget安装Entity Framwork和Json.net我们待会要使用这两个框架,顺便修改下配置文件

<connectionStrings>
        <add name="WeChartContex" connectionString="server=.;database=WeChat;uid=sa;pwd=000000"
          providerName="System.Data.SqlClient" />
    </connectionStrings>

创建model

我们再次回到浏览器控制台,分析服务器返回的数据

根据返回数据,我们可以建类 Friend


[Key]
        public int Id { get; set; }
        public int Uin { get; set; }
        public string UserName { get; set; }
        public string NickName { get; set; }
        public string HeadImgUrl { get; set; }
        public int ContactFlag { get; set; }
        public int MemberCount { get; set; }
        public List<Friend> MemberList { get; set; }
        public string RemarkName { get; set; }
        public int HideInputBarFlag { get; set; }
        public int Sex { get; set; }
        public string Signature { get; set; }
        public int VerifyFlag { get; set; }
        public int OwnerUin { get; set; }
        public string PYInitial { get; set; }
        public string PYQuanPin { get; set; }
        public int StarFriend { get; set; }
        public int AppAccountFlag { get; set; }
        public int Statues { get; set; }
        public int AttrStatus { get; set; }
        public string Province { get; set; }
        public string City { get; set; }
        public string Alias { get; set; }
        public int SnsFlag { get; set; }
        public int UniFriend { get; set; }
        public string DisplayName { get; set; }
        public int ChatRoomId { get; set; }
        public string KeyWord { get; set; }
        public string EncryChatRoomId { get; set; }




 

类BaseResponse

public class BaseResponse
   {
       //"Ret": 0, "ErrMsg": ""
       public int Ret { get; set; }
       public string ErrMsg { get; set; }
   }

 

 

 类Respone

 public class Respone
    {
        public BaseResponse respoen { get; set; }
        public int MemberCount { get; set; }
        public List<Friend> MemberList { get; set; }
    }
 

创建数据库上下文

 public class WeChartContex:DbContext
    {
        public    DbSet<Friend> Fridens { get; set; }
    }

创建控制器Sprider

  给他添加一个数据库上下文对象

 

WeChartContex context = new WeChartContex();

这种强耦合的代码是不被推荐的,由于我们的Demo特别小,这里暂时这样写

 

 
public ActionResult GetFridendInformation()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?r=1480564845349&seq=0&skey=@crypt_20089e09_d38ecc170f273d2db91833e793677276");
            request.Method = "get";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0";
            request.Referer = "https://wx.qq.com/";
            // CookieContainer contain = new CookieContainer();

  
            request.Headers.Add("Cookie", "pgv_pvi=1499432960; pt2gguin=o1694675518; RK=; /=s7972417536; wxsid=tjF6UrJ2RcvNH76H; wxloadtime=1480564533_expired);

           using(Stream dataStream = response.GetResponseStream())
          {
           using( StreamReader reader = new StreamReader(dataStream))
            {
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            Respone   responsefronserver = JsonHelper.DeserializeToObject<Respone>(responseFromServer);
            foreach (var item in responsefronserver.MemberList)
            {
                if(item.VerifyFlag==0) context.Fridens.Add(item);
            } 
              }
         }
    request.Abort();
if (context.SaveChanges()>0) { return Content("ok"); }


            return Content("fail");
        }
 

基本思路是根据URL来创建一个HttpWebRequest对象,它是用http协议来请求的,我们来设置UserAgent,cookie,这里的cookie我做了删除,cookie应该即时取浏览器提取,保证它是新鲜的

 

 
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?r=1480564845349&seq=0&skey=@crypt_20089e09_d38ecc170f273d2db91833e793677276");
            request.Method = "get";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0";
            request.Referer = "https://wx.qq.com/";
             request.Headers.Add("Cookie", "pgv_pvi=149943")
 

接下来创建HttpWebResponse对象来获取数据,并进行反序列化

首先创建一个jsonhelp

 
public class JsonHelper
    {
        /// <summary>
        /// 对数据进行序列化
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SerializeToString(object value)
        {
            return JsonConvert.SerializeObject(value);
        }
        /// <summary>
        /// 反序列化操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeserializeToObject<T>(string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }
}
 
 

接下来对返回的数据进行反序列化

 

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            Respone   responsefronserver = JsonHelper.DeserializeToObject<Respone>(responseFromServer);
 

 

接下来保存到数据库中,我们只要朋友的信息

 
  foreach (var item in responsefronserver.MemberList)
            {
                if(item.VerifyFlag==0) context.Fridens.Add(item);
            } 
            if (context.SaveChanges()>0)
            {
                return Content("ok");
            }

            return Content("fail");

VerifyFlag为0就是个人账号,公众号不为0

运行程序,进入Sprider/GetFridendInformation,返回OK执行成功

我一共有116个微信好友

微信获取好友信息 - 微信导出好友数据分析,本教程讲价如何导出微信好友信息。轻松实现微信好友列表获取导出。

本项目已经上传到github上面:https://github.com/wangqifan/WeChatAnalyse

 

上一篇:微信好友提取器特点说明_微信好友群成员提取器简介

下一篇:微信好友列表提取软件 之 如何搜索微信群并加入

注册体验: