how to scrap the .js file data from the html source ?

Discussion in 'C#' started by Edwin_Pro_Net, Nov 11, 2009.

  1. #1
    I need to grab the data from the .js file into my DB thru the vs 2008 c#, but since the .js doesn't have the view source capability

    so, anyone can provide the solution with the sample to me ?

    you help is more appreciated on me.

    sample link like below

    http://1x2.nowgoal.com/218444.js
     
    Edwin_Pro_Net, Nov 11, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    So you want to get the contents of that file?
    First you need to include System.Net and System.IO
    and here is a function that will get the contents of the file.

    In c# :
    
        public string getSource(string strUrl)
        {
            try
            {
                string pageSource = "";
                WebResponse r = WebRequest.Create(new Uri(strUrl)).GetResponse();
                StreamReader sr = new StreamReader(r.GetResponseStream());
                do
                {
                    pageSource += sr.ReadToEnd();
                } while (sr.EndOfStream == false);
                r.Close();
                sr.Close();
                return pageSource;
            }
            catch (Exception ex) { return ""; }
        }
    
    Code (markup):
    I use a loop even when using the ReadToEnd function because I found that sometimes I was missing some of the page and it seemed to fix it.

    Oh and here it is in VB (I changed the function and variable names when converting to C#):
    
        Function GetWebPage(ByVal strURI As String) As String
            Try
                Dim r As WebResponse
                r = WebRequest.Create(New Uri(strURI)).GetResponse()
                Dim sr As New StreamReader(r.GetResponseStream())
                Do Until sr.EndOfStream
                    GetWebPage = sr.ReadToEnd
                Loop
                r.Close()
                sr.Close()
            Catch ex As Exception
            End Try
        End Function
    
    Code (markup):
     
    camjohnson95, Nov 11, 2009 IP