第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以從我下載的 HTML 文件中提取一些數(shù)據(jù)嗎?它里面有一些 JSON

我可以從我下載的 HTML 文件中提取一些數(shù)據(jù)嗎?它里面有一些 JSON

C#
交互式愛情 2022-12-31 10:30:40
這是我下載的 HTML 文件的鏈接https://drive.google.com/open?id=1z7A9U0qZSVtLMQDbsVtPyZVz9Zm73-ZQ從這個文件最后你可以看到一些這樣的數(shù)據(jù)<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 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經(jīng)驗 獲得超3個贊

第一種方法


如果您想要 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;

}


查看完整回答
反對 回復(fù) 2022-12-31
?
狐的傳說

TA貢獻1804條經(jīng)驗 獲得超3個贊

您可以使用HTLMAgilityPack等庫來解析 HTML 文檔,然后根據(jù)需要提取 JSON。



查看完整回答
反對 回復(fù) 2022-12-31
?
30秒到達戰(zhàn)場

TA貢獻1828條經(jīng)驗 獲得超6個贊

您下載的文件不是有效的 HTML,因為它是 React 視圖。因此,HTMLAgilityPack 之類的工具對您的幫助不大。


您可以嘗試使用諸如WebKit.NET 之類的無頭瀏覽器,看看您是否有運氣。在構(gòu)建最終 HTML 的過程中,您可能可以在某處插入。


除此之外,我能想到的唯一選擇是使用正則表達式從文件中獲取所需的數(shù)據(jù)。例如:


var regex = new Regex(@"(?<=data-react-props=""){.*}(?=<)");

var match = regex.Match(pageContents);

if (match.Success)

{

    foreach (var gr in match.Groups)

    {

        Console.WriteLine(gr);

    }

}


查看完整回答
反對 回復(fù) 2022-12-31
  • 3 回答
  • 0 關(guān)注
  • 127 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號