Finding Web Content Using C #

How to search for website source code using C #? hard to explain heres the source for this in python

import urllib2, re
word = "How to ask"
source = urllib2.urlopen("http://stackoverflow.com").read()
if re.search(word,source):
     print "Found it "+word
+3
source share
3 answers

If you want to access raw HTML from a web page, you need to do the following:

  • Use HttpWebRequest to connect to file
  • Open the connection and read the response stream in the line
  • Finding an answer to your content

So the code is something like:

string pageContent = null;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://example.com/page.html");
HttpWebResponse myres = (HttpWebResponse)myReq.GetResponse();

using (StreamReader sr = new StreamReader(myres.GetResponseStream()))
{
    pageContent = sr.ReadToEnd();
}

if (pageContent.Contains("YourSearchWord"))
{
    //Found It
}
+6
source

I assume this is as close as you get to C # in your python code.

using System;
using System.Net;

class Program
{
    static void Main()
    {
        string word = "How to ask";
        string source = (new WebClient()).DownloadString("http://stackoverflow.com/");
        if(source.Contains(word))
            Console.WriteLine("Found it " + word);
    }
}

I'm not sure if re.search (#, #) is case sensitive or not. If not you could use ...

if(source.IndexOf(word, StringComparison.InvariantCultureIgnoreCase) > -1)

instead.

+2
source

HTML- , :

string url = "http://someurl.com/default.aspx";
WebRequest webRequest=WebRequest.Create(url);
WebResponse response=webRequest.GetResponse();

Stream str=response.GetResponseStream();
StreamReader reader=new StreamReader(str);
string source=reader.ReadToEnd();

, .

0

Source: https://habr.com/ru/post/1703040/


All Articles