N
N
Nick V2014-05-29 13:52:54
.NET
Nick V, 2014-05-29 13:52:54

Parser of members of VK groups. Having trouble loading your groups?

29_05_2014_13_34_27.jpg
(semi-finished layout)
For development, I decided to write such a parser for community members in contact. Unable to upload list of my groups to listBox. Tell me what I'm doing wrong. How to make a request?

The code
private string GET(string Url)
        {
            var reqGet = WebRequest.Create(Url);
            var resp = reqGet.GetResponse();
            var stream = resp.GetResponseStream();
            var sr = new StreamReader(stream);
            var Out = sr.ReadToEnd();
            sr.Close();
            return Out;
        }

        private string POST(string Url, string Data)
        {
            ServicePointManager.Expect100Continue = false;
            var cookies = new CookieContainer();
            var reqPost = (HttpWebRequest) WebRequest.Create(Url);
            reqPost.CookieContainer = cookies;
            reqPost.Method = "POST";
            reqPost.Timeout = 100000;
            reqPost.ContentType = "application/x-www-form-urlencoded";
            byte[] sentData = Encoding.UTF8.GetBytes(Data);
            reqPost.ContentLength = sentData.Length;
            var sendStream = reqPost.GetRequestStream();
            sendStream.Write(sentData, 0, sentData.Length);
            sendStream.Close();
            using (var responseStream = reqPost.GetResponse().GetResponseStream())
            using (var reader = new StreamReader(responseStream))
            {
                string Out = reader.ReadToEnd();
                return Out;
            }

        }

        private void http_auth_vk(string login, string pass)
        {
            //*****************************
            //Получаем action_url
            //*****************************
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            string Out = GET("http://m.vk.com/");

            //*****************************
            //Парсим
            //*****************************
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(Out);
            var bodyNode = doc.DocumentNode.SelectSingleNode("//div[@class='form_item fi_fat']/form");
            var _result = bodyNode.Attributes["action"].Value;


            //*****************************
            //POST запрос
            //*****************************
            string data = "email=" + login + "&pass=" + pass;
            Out = POST(_result, data);
            
            
                //*****************************
                //Парсим, поиск ID
                //*****************************                 
                doc.LoadHtml(Out);
                try
                {
                    bodyNode = doc.DocumentNode.SelectSingleNode("//h2[@class='op_header']/a");
                    labelUserInfo.Text = bodyNode.InnerText + " - ";
                    bodyNode = doc.DocumentNode.SelectSingleNode("//div[@class='ip_user_link']/a");
                    labelUserInfo.Text += "ID " + bodyNode.Attributes["href"].Value.Substring(3);
                    bodyNode = doc.DocumentNode.SelectSingleNode("//div[@class='ip_user_link']/a/img");
                    userImg.Load(bodyNode.Attributes["src"].Value);
                    bodyNode = doc.DocumentNode.SelectSingleNode("//ul[@class='left_footer_menu']/li[4]/a");
                    _logOut = bodyNode.Attributes["href"].Value;
                    //Если ID найден, то авторизация удалась  
                    LoadGroup();
                }
                catch
                {
                    //Если ID не найден, то авторизация не удалась
                    MessageBox.Show("Авторизация не удалась. Возможно там капча=(");
                    this.buttonCnct.Enabled = true;
                    this._login.Enabled = true;
                    this._pass.Enabled = true;
                }
            
        }

        private  void LoadGroup()
        {

            string Out = ;

                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(Out);
                HtmlNodeCollection nodes =
                    doc.DocumentNode.SelectNodes("//div[@id='gr_search_items']/a[@class='simple_fit_item']");
                foreach (var node in nodes)
                {
                    
                }
            
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan, 2014-05-29
@L3n1n

Why are you parsing m.vk.com if everything you need can be obtained through the API?
https://vk.com/dev/groups.get
UPD.:
You obviously didn't show all the code.
To get a list of groups, you need to make a POST to the address m.vk.com/groups?tab=groups
In your code, I see only authorization and LoadGroup () which should parse HTML which is not. Why not? Yes, because it is generated by Ajax in response to POST m.vk.com/groups?tab=groups

N
Nick V, 2014-05-29
@half-life

How would you explain that. Can. But now it is not necessary. Now I want to know how to make a request correctly. Through the API, I will make a similar parser when I'm done with this. What for? Practice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question