【问题标题】:How do i get all the text from the string in a loop and not only once?我如何从循环中的字符串中获取所有文本,而不仅仅是一次?
【发布时间】:2012-08-11 02:35:56
【问题描述】:

我有这个代码:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            WebRequest request = WebRequest.Create(url);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string content = reader.ReadToEnd();
            reader.Close();
            response.Close();

        }

现在我有两个功能:

private void GetProfileNames(string text)
        {
            string startTag = "<a  href='/profile/";
            string endTag = "'>";
            int startTagWidth = startTag.Length;
            int endTagWidth = endTag.Length;
            index = 0;
            while (true)
            {
                index = text.IndexOf(startTag, index);
                if (index == -1)
                {
                    break;
                }
                // else more to do - index now is positioned at first character of startTag 
                int start = index + startTagWidth;
                index = text.IndexOf(endTag, start + 1);
                if (index == -1)
                {
                    break;
                }
                // found the endTag 
                profileName = text.Substring(start, index - start);
            }
            return profileName;
        }

        private void GetTextFromProfile(string text)
        {
            string str = "<span class=\"message-text\">";
            string startTag = str;
            string endTag = "<";
            int startTagWidth = startTag.Length;
            int endTagWidth = endTag.Length;
            index = 0;
            while (true)
            {
                index = text.IndexOf(startTag, index);
                if (index == -1)
                {
                    break;
                }
                // else more to do - index now is positioned at first character of startTag 
                int start = index + startTagWidth;
                index = text.IndexOf(endTag, start + 1);
                if (index == -1)
                {
                    break;
                }
                // found the endTag 
                profileNameText = text.Substring(start, index - start);
            }

            return profileNameText;
        }

现在,在 DoWork 事件中的字符串内容行之后,我调用了函数:GetProfileNames,但是当我在该行上使用断点时:profileNameText = text.Substring(start, index - start);我总是得到相同的配置文件名称,我需要关闭程序再次运行它。

我想要它做的是当我在 Dowork 事件中调用该函数时,它将使 GetProFileNames 函数结束,并从当前已下载的内容中获取所有配置文件名称。


不知何故,我需要调用这两个函数:GetProfileNames 和 GetTextFromProfile,并且我需要为每个配置文件和属于他的文本创建一个字符串。

例如我在内容变量中有这一行:

<span class="message-profile-name" ><a  href='/profile/LipazD'>LipazD</a></span>: <span class="message-text">hello world</span>

所以我需要这两个函数循环遍历内容,每次迭代时我都会得到一个字符串,如 string t = "LipazD hello world" 下一个迭代将是:“丹尼尔你好吗?”

函数工作他们获取配置文件名称,第二个获取文本,但我不知道如何进行迭代循环并使其全部工作。


然后,当它完成循环内容并获取每个配置文件名称的所有配置文件名称和文本时,我需要删除内容并再次下载新内容,然后使用功能完成删除内容或只下载一个一遍又一遍的新内容等等。

【问题讨论】:

  • 我不明白你的问题出在哪里,但是你不能看看HTML Agility Pack解析HTML吗?
  • 正如@CodeCaster 所说:我还建议使用 HTML Agility Pack 并将其与 WebClient.DownloadString 方法结合使用:msdn.microsoft.com/en-us/library/fhd1f0sw.aspx
  • 我同意@CodeCaster。手动解析 HTML/XML 几乎从来都不是一个好主意。为此有大量强大而有效的库。
  • 我不知道如何使用 html aility pack 读了几次,我不明白如何将它与我的代码一起使用来解析内容并重新构建它。我不理解 html 敏捷包的所有标志、节点和内容。
  • 我猜使用 html 敏捷包应该很容易。

标签: c#


【解决方案1】:
HtmlDocument doc = new HtmlDocument();
WebClient wc = new WebClient();

doc.Load(wc.DownloadString("http://yourUri.com"));
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//span[@class='message-profile-name'"])
{
    // etc.
}

但我认为 message-profile-name 和 message-text 包含在父元素中。我建议遍历该元素,然后获取子配置文件名称和评论范围内容

【讨论】:

    【解决方案2】:
    var wc = new WebClient();
    
    wc.DownloadStringCompleted += (s, e) =>
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(e.Result);
    
        var link = doc.DocumentNode
                        .SelectSingleNode("//span[@class='message-profile-name']")
                        .Element("a")
                        .Attributes["href"].Value;
    };
    
    wc.DownloadStringAsync(new Uri("http://chatroll.com/rotternet"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2014-04-24
      • 1970-01-01
      • 2019-06-29
      • 2021-01-08
      • 2011-07-08
      • 2020-10-27
      相关资源
      最近更新 更多