我怎样才能获得这个函数的变量?

时间:2014-07-09 16:07:59

标签: c# javascript parsing web html-agility-pack

页面有:

 <script type="text/javascript">
     $(function() {
          var articleId = '28';
          var data = window.articleDetailData = {
      "articleData": {
      "id": 20,
      "userId": 73,
      "postId": 29,
      "hurArticleId": 26656,
      "aTitle": "sometext",
      "aTitleShort": "sometext",
      "aSpotText": "“sometext",
      "aSpotTextSort": "“sometext",
      "aContent": "the Text I want",
      "addDate": "2014"
                     },
    "userData": {
     "id": 28049,
    "isUserLiked": false,
   "isUserShared": false
    ......

我想获得此JavaScript的aContent变量。我试过了:

doc.DocumentNode.SelectSingleNode("//script[contains(text(), 'aContent')]").InnerHtml
  doc.DocumentNode.SelectSingleNode("*//script[@type='text/javascript']");

并且还有噪音javascript nuget。无法实现它。我想这不能用agilitypack解析。

我该如何开始?

2 个答案:

答案 0 :(得分:1)

对我来说,这看起来像json。在这种情况下,你可以这样做:

var content = data.articleData[0].aContent;

答案 1 :(得分:-2)

您可以使用此正则表达式模式解析它:

(?<=\"aContent\": \")(.*?)(?=\",)