How do you search a websites source code with C#? hard to explain, heres the source for doing it 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
If you want to access the raw HTML from a web page you need to do the following:
So code 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
}
I guess this is as close as you'll get in C# to 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 it's not you could use...
if(source.IndexOf(word, StringComparison.InvariantCultureIgnoreCase) > -1)
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With