【问题标题】:C# Extract names with HttpWebRequests [closed]C# 使用 HttpWebRequests 提取名称 [关闭]
【发布时间】:2012-11-10 23:20:44
【问题描述】:

我是动漫迷,我想获得所有动漫角色的完整列表,所以我遇到了这个网站: http://www.animevice.com/characters/?page=1 我的目标是提取所有名称并将它们添加到 listBox1。这是我当前的代码:

        try
        {
        while (true)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.animevice.com/characters/?page=" + n);
            req.Method = "GET";
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0";
            req.KeepAlive = true;

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            Stream responseData = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseData);
            string responseFromServer = reader.ReadToEnd();
            string m = "<a href=\"(.*)\" class=\"name\">(.*)</a>";
            Match match = Regex.Match(responseFromServer, m, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                listBox1.Items.Add(match.Groups[2]Value.ToString());

            }
            if (listBox1.Items.Count % 50 == 0)
            {
                n++;
            }
        }
}
catch { }

但是,这多次只给了我名单上的第一个名字(Monkey D. Luffy)。 有什么解决办法吗? 干杯

【问题讨论】:

  • 不确定这是否是一个 winforms,但您可以使用 WebBrowser 类,它可以让您更多地访问 DOM,从而让您更轻松地查询锚对象。
  • 是的,它是 Windows 窗体应用程序。感谢您的建议,如果我没有找到答案,我会尝试一下。

标签: c# web response extract names


【解决方案1】:

我会使用像 HtmlAgilityPack 这样的真正的 html 解析器来解析 html 而不是正则表达式。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(responseFromServer);
var names = doc.DocumentNode.SelectNodes("//a[@class='name']")
                .Select(a=>a.InnerText)
                .ToList();

listBox1.DataSource = names;

【讨论】:

    【解决方案2】:

    您只读取了一个页面名称。

    改为:

    Match match = Regex.Match(responseFromServer, m, RegexOptions.IgnoreCase);
    if (match.Success)
    {
        listBox1.Items.Add(match.Groups[2]Value.ToString());
    
    }
    if (listBox1.Items.Count % 50 == 0)
    {
        n++;
    }
    

    试试这个:

    var matches = Regex.Matches(responseFromServer, m, RegexOptions.IgnoreCase);
    foreach (var item in matches)
    {
        var match = item as Match;
        if (match.Success)
        {
            listBox1.Items.Add(match.Groups[2]Value.ToString());    
        }
        if (list.Count % 50 == 0)
        {
            n++;
        }
    }
    

    【讨论】:

    • 这行得通,谢谢!我只是对其进行了一些编辑,使用 MatchCollection 而不是 'var' 因为我不知道它的功能:3 干杯,感谢您帮助我。
    • @user1815324:不客气。但是您应该尝试更好的方法,例如answer from L.B
    【解决方案3】:
    using (StreamReader reader = new StreamReader(responseData))
      {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                 string m = "<a href=\"(.*)\" class=\"name\">(.*)</a>";
                 Match match = Regex.Match(line, m, RegexOptions.IgnoreCase);
                 if (match.Success)
                 {
                     listBox1.Items.Add(match.Groups[2].Value.ToString());
                 }
             }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      相关资源
      最近更新 更多