0

这是完整的 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}-->

如果我必须为oidvar 获取值(567334127898),接下来我需要做什么?

4

2 回答 2

0

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

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

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

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

于 2013-03-24T17:49:16.547 回答
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();
于 2013-03-24T18:47:57.640 回答