从html页面源中提取数据

时间:2016-06-30 15:48:38

标签: c# html regex

我需要从网站中提取某些数据。

我看过这个youtube视频 https://www.youtube.com/watch?v=rru3G7PLVjw 并且大致了解如何编码。

基本上我想要做的是提取和存储(单选按钮文本)非常简单!,非常简单,不容易列入列表

来自https://docs.google.com/forms/d/1Mout_ImbF9N16EuCiYOxCrL6MbkUVkIEzijO1PAUQ68/viewform?key=pqbhTz7PIHum_4qKEdbUWVg

的页面来源

然后打印出列表中的元素

以下是我根据youtube视频编写的c#代码。

using System.Net;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ExtractDataFromWebsite
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> radioOptions = new List<string>();
            WebClient web = new WebClient();

            // download html from certain website
            string html = web.DownloadString("https://docs.google.com/forms/d/1Mout_ImbF9N16EuCiYOxCrL6MbkUVkIEzijO1PAUQ68/viewform?key=pqbhTz7PIHum_4qKEdbUWVg");

            MatchCollection m1 = Regex.Matches(html, @"<input\stype=/"radio"\sname=/"entry.2362106 / "\svalue="(.+)\sid =/ "group_2362106_"
                , RegexOptions.Singleline);
            foreach (Match m in m1)
            {
                    string radioOption = m.Groups[1].Value;
                    radioOptions.Add(radioOption);
            }
            for (int i=0; i< radioOptions.Count;i++)
                Console.WriteLine(""+ radioOptions[i]);

            Console.ReadKey();
        }
    }
}

然而,MatchCollection m1 = Regex.Matches ......有一些问题,我不知道如何修复。

希望有人可以提供一些提示或帮助来解决上述问题 非常感谢你

2 个答案:

答案 0 :(得分:0)

查看HtmlAgilityPack。您可以将webclient响应中的源加载到新的htmldocument中,并从那里轻松遍历它。

答案 1 :(得分:0)

尝试使用此正则表达式作为值提取器:

MatchCollection m1 = Regex.Matches(html, "<input type=\"radio\".+?value=\"(.+?)\".+?\">"
            , RegexOptions.Singleline);
相关问题