使用C#搜索Web内容

loc*_*ost 3 c#

你如何用C#搜索网站源代码?很难解释,继承人在python中做到这一点

import urllib2, re
word = "How to ask"
source = urllib2.urlopen("http://stackoverflow.com").read()
if re.search(word,source):
     print "Found it "+word
Run Code Online (Sandbox Code Playgroud)

Wol*_*yrd 7

如果要从网页访问原始HTML,则需要执行以下操作:

  1. 使用HttpWebRequest连接到该文件
  2. 打开连接并将响应流读入字符串
  3. 搜索您的内容的回复

所以代码如下:

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
}
Run Code Online (Sandbox Code Playgroud)