Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用c语言阅读网站内容?_C#_Html_Httpwebrequest_Webclient_Streamreader - Fatal编程技术网

C# 如何用c语言阅读网站内容?

C# 如何用c语言阅读网站内容?,c#,html,httpwebrequest,webclient,streamreader,C#,Html,Httpwebrequest,Webclient,Streamreader,我想阅读没有html标签和标题的网站文本。我只需要在web浏览器中显示文本 我不需要这样 <html> <body> bla bla </td><td> bla bla <body> <html> 我只需要这段文字 我使用webclient和httpwebrequest方法来获取HTML内容并分割接收到的数据,但这是不可能的,因为如果我更改网站,标签可能会更改 那么,有没有办法只按语法获取网站中显示的文本?您需要使用特殊

我想阅读没有html标签和标题的网站文本。我只需要在web浏览器中显示文本

我不需要这样

<html>
<body>
bla bla </td><td>
bla bla 
<body>
<html>
我只需要这段文字

我使用webclient和httpwebrequest方法来获取HTML内容并分割接收到的数据,但这是不可能的,因为如果我更改网站,标签可能会更改


那么,有没有办法只按语法获取网站中显示的文本?

您需要使用特殊的HTML解析器。获得这种非正规语言内容的唯一方法

// Reading Web page content in c# program
//Specify the Web page to read
WebRequest request = WebRequest.Create("http://aspspider.info/snallathambi/default.aspx");
//Get the response
WebResponse response = request.GetResponse(); 
//Read the stream from the response
StreamReader reader = new StreamReader(response.GetResponseStream()); 
//Read the text from stream reader
string str = reader.ReadLine();
for(int i=0;i<200;i++)
{
   str += reader.ReadLine();

}

Console.Write(str);
请看:

我想我可以帮你

/// <summary>
/// Remove HTML tags from string using char array.
/// </summary>
public static string StripTagsCharArray(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;

for (int i = 0; i < source.Length; i++)
{
    char let = source[i];
    if (let == '<')
    {
    inside = true;
    continue;
    }
    if (let == '>')
    {
    inside = false;
    continue;
    }
    if (!inside)
    {
    array[arrayIndex] = let;
    arrayIndex++;
    }
}
return new string(array, 0, arrayIndex);
}

下面是如何使用

首先是示例HTML:

var html = "<html>\r\n<body>\r\nbla bla </td><td>\r\nbla bla \r\n<body>\r\n<html>";
如果从web上获取,则类似:

var web = new HtmlWeb();
var doc = web.Load(url);
现在,只选择带有非空白的文本节点并修剪它们

var text = doc.DocumentNode.Descendants()
              .Where(x => x.NodeType == HtmlNodeType.Text && x.InnerText.Trim().Length > 0)
              .Select(x => x.InnerText.Trim());
如果愿意,可以将其作为单个连接字符串获取:

String.Join(" ", text)

当然,这只适用于简单的网页。任何复杂的操作都会返回包含您显然不需要的数据的节点,如javascript函数等。

您不能将HTML视为简单文本或常规表达式,它不是一种常规的文本或语言。@jaiff::请详细说明最后一个循环,为什么只读取200个索引。我认为您需要一个HTML解析器,如果您可以控制页面源代码,则可以向要获取的元素添加一个id,所以,要使用语法分析器的getElementById这样的方法来获取它。@alfoks::您有HTML语法分析器的示例链接吗?但这是一种方法,您可以得到您想要的@Azemakram:使用你可以得到你感兴趣的值。最后,这是一个解析器:::我如何像处理循环中的字符串数组那样,逐个索引访问文本索引,例如'fori=0;iYou可以直接针对文本执行此操作:文本中的foreach var index{//使用index做点什么}。或者,您可以执行text.ToArray;正则表达式不应该被用来解析HTML作者给了你3种方法。最后一个建议是StripTagsCharArray,如果在一些嵌入式JavaScript中遇到if语句(如“Ifx<4”),您认为该方法将如何处理?答案是:不太好。正确的答案是HtmlAgilityPack。
public string GetwebContent(string urlForGet)
{
    // Create WebClient
    var client = new WebClient();
    // Download Text From web
    var text = client.DownloadString(urlForGet);
    return text.ToString();
}
public string GetwebContent(string urlForGet)
{
    // Create WebClient
    var client = new WebClient();
    // Download Text From web
    var text = client.DownloadString(urlForGet);
    return text.ToString();
}