我可以从下载的HTML文件中提取一些数据吗?

时间:2019-04-25 08:24:35

标签: c# json html-agility-pack read-write

这是我下载的HTML文件的链接

https://drive.google.com/open?id=1z7A9U0qZSVtLMQDbsVtPyZVz9Zm73-ZQ

最后从该文件中您可以看到类似这样的数据

<div data-react-class="packs/v9/phone/containers/AreaCodeListing" data-react-props="{"areaCodes":[{"phone_prefix":"(202) 200","details":["Sprint"],"location":"Washington, DC","href":"/202-200"},{"phone_prefix":"(202) 201","details":["Verizon"],"location":"Washington, DC","href":"/202-201"},{"phone_prefix":"(202) 202","details":["General Service Carrier"],"location":"Washington, DC","href":"/202-202"},{"phone_prefix":"(202) 203","details":["T-Mobile"],"location":"Washington, DC","href":"/202-203"},{"phone_prefix":"(202) 204","details":["XO Communications"],"location":"Washington, DC","href":"/202-204"}

如何从此页面提取 href 值? 我认为 JSON 可以完成这项工作,但我一直坚持如何获取该JSON

或者是否有其他最佳方法从我下载的HTML页面中获取 href 值?

3 个答案:

答案 0 :(得分:0)

您可以使用HTLMAgilityPack之类的库来解析HTML文档,然后根据需要提取出JSON。

答案 1 :(得分:0)

您下载的文件不是有效的HTML,因为它是一个React视图。 因此,像HTMLAgilityPack这样的工具对您不是很有帮助。

您可以尝试使用WebKit.NET之类的无头浏览器来查看自己是否有运气。在构建最终HTML的过程中,您也许可以插入某个地方。

除此之外,我唯一想到的选择是使用正则表达式从文件中获取所需的数据。例如:

var regex = new Regex(@"(?<=data-react-props=""){.*}(?=<)");
var match = regex.Match(pageContents);
if (match.Success)
{
    foreach (var gr in match.Groups)
    {
        Console.WriteLine(gr);
    }
}

答案 2 :(得分:0)

第一种方法

如果要使用AreaCode的整个对象,请尝试第一种方法。

public List<AreaCode> GetAllAreaCodes(string htmlString)
{

    List<AreaCode> areraCodes = new List<AreaCode>();

    Regex rgxAttr = new Regex(@"data-react-props=""{(.*?)}""");
    Regex rgxValue = new Regex(@"""{(.*?)}""");


    var attrResult = rgxAttr.Matches(htmlString);
    List<string> attrValues = new List<string>();

    foreach (Match match in attrResult)
    {
        var val = rgxValue.Match(match.Value);
        attrValues.Add(val.Value.Replace("\"{", "{").Replace("}\"", "}"));
    }

    foreach (var item in attrValues)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();

        var dn = js.Deserialize<dynamic>(item) as Dictionary<string, object>;

        if (dn != null && dn.ContainsKey("areaCodes"))
        { 
            var abc = item.Remove(item.Length - 1, 1).Remove(0, 1).Replace(@"""areaCodes"":", "");
            areraCodes = js.Deserialize<List<AreaCode>>(abc);
        }
    }
    return areraCodes;
}
public class AreaCode
{
    public string phone_prefix { get; set; }
    public string location { get; set; }
    public string href { get; set; }
    public string[] details { get; set; }

}

第二种方法

如果仅需要href值,则使用第二种方法。

public List<string> GetAllHref(string htmlString)
{

    List<string> hrefList = new List<string>();

    Regex rgxAttr = new Regex(@"data-react-props=""{(.*?)}""");
    Regex rgxValue = new Regex(@"""{(.*?)}""");

    var attrResult = rgxAttr.Matches(htmlString);

    List<string> attrValues = new List<string>();

    foreach (Match match in attrResult)
    {
        var val = rgxValue.Match(match.Value);
        attrValues.Add(val.Value.Replace("\"{", "{").Replace("}\"", "}"));
    }

    dynamic ob = null;
    foreach (var item in attrValues)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        var dn = js.Deserialize<dynamic>(item) as Dictionary<string, object>;
        if (dn != null && dn.ContainsKey("areaCodes"))
            ob = dn["areaCodes"];
    }

    var s = ob as Array;
    foreach (Dictionary<string, object> item in s)
        hrefList.Add(item["href"].ToString());

    return hrefList;
}
相关问题