Html敏捷包解析oid

时间:2013-03-24 17:39:30

标签: c# html html-agility-pack

以下是完整的HTML代码:http://pastebin.com/WLwDCVZ0

我使用敏捷包解析了所需的节点。代码:

var html = new HtmlDocument();
html.LoadHtml(request.Get("http://www.odnoklassniki.ru").ToString());
var h1 = html.DocumentNode.SelectSingleNode(@"/html[1]/body[1]/div[1]/div[3]/div[2]");

h1包含:

<!--{"uid":"aocrgdowcvpgtiymx0qqvblirnscijwdrcw","toolbar":true,"restrictedNIM":false,"restrictedMusic":false,"massUploadFix":true,"valentineActive":false,"navFactoryReqTimeout":3000,"discussionsRestricted":false,"share.urlPattern":"((?:(?:https?|ftp)://|(?:mailto:)?[-\\w!#$%\u0026'*+/\u003d?^`{|}~][-\\w!#$%\u0026'*+/\u003d?^`{|}~\\.]*@)?(?:(?:(?:(?:[\\wа-яА-Я-]+)\\.){1,5})(?:com|net|org|biz|info|name|pro|asia|aero|cat|coop|eco|jobs|mobi|museum|post|tel|travel|xxx|edu|gov|int|mil|рф|испытание|xn--[\\w-]*|[a-z]{2})|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?::\\d{1,5})?(?:/(?:[\\w/а-яА-Я\\-\\.,/~!#$%\u0026*+;:\u003d?\\[\\]'@()]*[\\wа-яА-Я/])?)?)","friendOnlinePopupDuration":25000,"premIc":true,"tinkoffPromoLink":"http://www.odnoklassniki.ru/apps/Tinkoff?tcsbanner\u003dodn_ok_bonus","valentineSamePresentIconRotateDelay":100,"configToolbarShowSpamExplanation":false,"lastName":"Иванова","menuWait":100,"configToolbarNotificationsEnableUnsubscribeAppEventsButton":true,"video.player.url":"http://stg.odnoklassniki.ru/static/odklvp/0-0-83/odklvp.swf?preferHd\u003d1\u0026noUpload\u003d1","menuBrief":true,"menuShow":500,"d_disc_ids":{"USER_ALBUM":6,"MOVIE":9,"USER_PHOTO":4,"SCHOOL_FORUM":10,"CITY_NEWS":13,"GROUP_TOPIC":1,"USER_FORUM":5,"USER_2LVL_FORUM":7,"SHARE":8,"GROUP_MOVIE":12,"GROUP_PHOTO":2,"HAPPENING_TOPIC":11,"CHAT":14,"USER_STATUS":3},"n.f.collapseEnabled":false,"pcId":"aocrgdowcvpgtiymx0aosttobwcoktbnwll","maxUsersInPopup":3,"notificationsRestricted":false,"toolbarNotificationsHideEnabled":true,"showSendOverlayLink":false,"visaOnlinePromoLink":"http://odnoklassniki.ru/apps/online-payments-test?view\u003dvisapromo","video.up-meta.url":"http://in.video.mail.ru/","groupOffComments":true,"displayCloseLinkWithMFVisaPromo":true,"configHintEnter":false,"communitySpamEnabled":true,"isUSt":false,"restrictedDisc":false,"menuStrict":true,"mms.groupIds":["aocrgdowcvpgtiymx0rgqrvvkoexjbmjrgp","aocrgdowcvpgtiymx0rmdrfqrjqyjhmhkgts","aocrgdowcvpgtiymx0rbuixcrnynjvsiqopz"],"app.feature.openlinks.enabled":true,"oid":"567334127898","menuClose":100,"bannerTargeting":{"v":{"d":"25","r":"307","n":"Анжела","m":"1","i":"567334127898"},"u":"AAAAhBe/yRoAAAE9nXB67Xpb+4YKHvKyVcA+tuqR8z8\u003d","t":"obLD1AAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAQAAAAAABkBxQcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAc\u003d"},"firstName":"Анжела","configToolabrRemoveConversation":true,"mpid":{"photoId":"491764025626"},"layoutConfig":"313542320128","friendOnlinePopupPosition":"right","dEnbl":true,"lbaidl":true,"male":false,"app.feature.openlinks.statuses.enabled":true,"overlayCategory":"1","restricted":false,"plainTooltips":true,"configHintSound":false,"dDelay":300000,"dItClosedSz":0,"dItSz":3,"dDelayPd":600000,"hasWishList":false,"overlayLinkGenderMask":2,"configToolbarSmilesVersion2":true}-->

如果我必须为变量获得oid值(567334127898),我需要做什么?

2 个答案:

答案 0 :(得分:0)

我假设oid只能包含数字?如果我错了,请纠正我。

var match = Regex.Match(h1,"\"oid\":\"(?<oid>\\d+)\"");

string oid = match.Groups["oid"].Value;
Console.WriteLine(oid);

其中h1是您使用Html Agility Pack解析的节点内容(作为字符串)

答案 1 :(得分:0)

HTML评论中有一个JSON字符串。

您可以使用Newtonsoft.Json库。

删除html评论字符串:<!-- -->

然后你可以提取这样的数字:

String JsonStr = "{\"uid\":\"aocrgdowcvpgtiymx0qqvblirnscijwdrcw\",\"toolbar\":true,\"restrictedNIM\":false,\"restrictedMusic\":false,\"massUploadFix\":true,\"valentineActive\":false,\"navFactoryReqTimeout\":3000,\"discussionsRestricted\":false,\"share.urlPattern\":\"((?:(?:https?|ftp)://|(?:mailto:)?[-\\\\w!#$%\\u0026'*+/\\u003d?^`{|}~][-\\\\w!#$%\\u0026'*+/\\u003d?^`{|}~\\\\.]*@)?(?:(?:(?:(?:[\\\\w\u0430-\u044F\u0410-\u042F-]+)\\\\.){1,5})(?:com|net|org|biz|info|name|pro|asia|aero|cat|coop|eco|jobs|mobi|museum|post|tel|travel|xxx|edu|gov|int|mil|\u0440\u0444|\u0438\u0441\u043F\u044B\u0442\u0430\u043D\u0438\u0435|xn--[\\\\w-]*|[a-z]{2})|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?::\\\\d{1,5})?(?:/(?:[\\\\w/\u0430-\u044F\u0410-\u042F\\\\-\\\\.,/~!#$%\\u0026*+;:\\u003d?\\\\[\\\\]'@()]*[\\\\w\u0430-\u044F\u0410-\u042F/])?)?)\",\"friendOnlinePopupDuration\":25000,\"premIc\":true,\"tinkoffPromoLink\":\"http://www.odnoklassniki.ru/apps/Tinkoff?tcsbanner\\u003dodn_ok_bonus\",\"valentineSamePresentIconRotateDelay\":100,\"configToolbarShowSpamExplanation\":false,\"lastName\":\"\u0418\u0432\u0430\u043D\u043E\u0432\u0430\",\"menuWait\":100,\"configToolbarNotificationsEnableUnsubscribeAppEventsButton\":true,\"video.player.url\":\"http://stg.odnoklassniki.ru/static/odklvp/0-0-83/odklvp.swf?preferHd\\u003d1\\u0026noUpload\\u003d1\",\"menuBrief\":true,\"menuShow\":500,\"d_disc_ids\":{\"USER_ALBUM\":6,\"MOVIE\":9,\"USER_PHOTO\":4,\"SCHOOL_FORUM\":10,\"CITY_NEWS\":13,\"GROUP_TOPIC\":1,\"USER_FORUM\":5,\"USER_2LVL_FORUM\":7,\"SHARE\":8,\"GROUP_MOVIE\":12,\"GROUP_PHOTO\":2,\"HAPPENING_TOPIC\":11,\"CHAT\":14,\"USER_STATUS\":3},\"n.f.collapseEnabled\":false,\"pcId\":\"aocrgdowcvpgtiymx0aosttobwcoktbnwll\",\"maxUsersInPopup\":3,\"notificationsRestricted\":false,\"toolbarNotificationsHideEnabled\":true,\"showSendOverlayLink\":false,\"visaOnlinePromoLink\":\"http://odnoklassniki.ru/apps/online-payments-test?view\\u003dvisapromo\",\"video.up-meta.url\":\"http://in.video.mail.ru/\",\"groupOffComments\":true,\"displayCloseLinkWithMFVisaPromo\":true,\"configHintEnter\":false,\"communitySpamEnabled\":true,\"isUSt\":false,\"restrictedDisc\":false,\"menuStrict\":true,\"mms.groupIds\":[\"aocrgdowcvpgtiymx0rgqrvvkoexjbmjrgp\",\"aocrgdowcvpgtiymx0rmdrfqrjqyjhmhkgts\",\"aocrgdowcvpgtiymx0rbuixcrnynjvsiqopz\"],\"app.feature.openlinks.enabled\":true,\"oid\":\"567334127898\",\"menuClose\":100,\"bannerTargeting\":{\"v\":{\"d\":\"25\",\"r\":\"307\",\"n\":\"\u0410\u043D\u0436\u0435\u043B\u0430\",\"m\":\"1\",\"i\":\"567334127898\"},\"u\":\"AAAAhBe/yRoAAAE9nXB67Xpb+4YKHvKyVcA+tuqR8z8\\u003d\",\"t\":\"obLD1AAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAQAAAAAABkBxQcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAc\\u003d\"},\"firstName\":\"\u0410\u043D\u0436\u0435\u043B\u0430\",\"configToolabrRemoveConversation\":true,\"mpid\":{\"photoId\":\"491764025626\"},\"layoutConfig\":\"313542320128\",\"friendOnlinePopupPosition\":\"right\",\"dEnbl\":true,\"lbaidl\":true,\"male\":false,\"app.feature.openlinks.statuses.enabled\":true,\"overlayCategory\":\"1\",\"restricted\":false,\"plainTooltips\":true,\"configHintSound\":false,\"dDelay\":300000,\"dItClosedSz\":0,\"dItSz\":3,\"dDelayPd\":600000,\"hasWishList\":false,\"overlayLinkGenderMask\":2,\"configToolbarSmilesVersion2\":true}";

Newtonsoft.Json.Linq.JContainer Properties = Newtonsoft.Json.JsonConvert.DeserializeObject(JsonStr) as Newtonsoft.Json.Linq.JContainer;

Newtonsoft.Json.Linq.JToken jToken = Properties.SelectToken("oid");

String Number = jToken.ToString();