none
scraping c# RRS feed

  • Pregunta

  • Hola necesito realizar scraping c# sobre una web. Logre realizar el login . Pero no se como navegar sobre el sitio. con el

    fin de descargar un documento. Todo por consola

    Gracias.

    Codigo

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;


    namespace scraping
    {
        class Program
        {
            static void Main(string[] args)
            {
                var cookieContainer = new CookieContainer();

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://xxxxxxt/login");
                request.CookieContainer = cookieContainer;
                //set the user agent and accept header values, to simulate a real web browser
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

                Console.WriteLine("FIRST RESPONSE");
                Console.WriteLine();
                using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    {
                        Console.WriteLine(sr.ReadToEnd());
                    }
                }

                request = (HttpWebRequest)HttpWebRequest.Create("https://xxxxx/login");
                //set the cookie container object
                request.CookieContainer = cookieContainer;
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

                //set method POST and content type application/x-www-form-urlencoded
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                //SET AUTOMATIC DECOMPRESSION
                //request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

                //insert your username and password
                string data = string.Format("usuario={0}&password={1}", "admin", "1412");
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);

                request.ContentLength = bytes.Length;

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(bytes, 0, bytes.Length);
                    dataStream.Close();
                }

                WebClient w = new WebClient();
               
               

                //var hrefs = aTags.Descendants("a")
                //.Select(node => node.GetAttributeValue("href", ""))
                //.ToList();

                Console.WriteLine("LOGIN RESPONSE");
                Console.WriteLine();


    Por favor ayuda

    Gracias

    martes, 15 de enero de 2019 19:45

Respuestas

Todas las respuestas