简体   繁体   English

如何获取Web图形的javascript标记内的数据,以便创建自己的图形?

[英]How can I get the data inside a javascript tag for a web graph, so I can make my own graph?

So far using the python libraries twill and beautifulsoup I was able to log into my utility company's website, and get to the page with my usage data for gas and electricity respectively. 到目前为止,使用python库twillbeautifulsoup,我能够登录到公用事业公司的网站,并分别显示我的天然气和电力使用数据页面。 They actually made a nice plot with javascript, and the raw data is available buried in there. 他们实际上使用javascript绘制了一个漂亮的图,并且原始数据可以在其中找到。

The twill library very nicely displays the composition of web forms #1, #2, #3, etc using showforms() , show() , etc, but not javascript elements so far as I can tell. 斜纹库使用showforms()show()等很好地显示了#1,#2,#3等Web表单的组成,但据我所知,不是javascript元素。 beautifulsoup seems to be able to display html elements, like the title of the webpage eg: title1 = soup1.html.head.title , but I can't seem to get it to list the javascript elements (there are like 4 or 5 different javascripts on the page, one of which contains the data below). beautifulsoup似乎能够显示html元素,例如网页标题,例如: title1 = soup1.html.head.title ,但是我似乎无法列出它来列出javascript元素(有4种或5种不同页面上的javascripts,其中之一包含以下数据)。

Is there any way to use something like showjavascript() or soup1.html.body.javascript in twill or beautifulsoup respectively, to access this javascript data and perhaps parse it for import into matplotlib or other python plotting package? 有什么方法可以分别在twillbeautifulsoup中使用诸如showjavascript()soup1.html.body.javascript类的东西来访问此javascript数据,并可能解析它以导入到matplotlib或其他python绘图包中?

the data in the javascript element looks like this (you can see me, the neighbors, and the efficient neighbors): javascript元素中的数据如下所示(您可以看到我,邻居和有效邻居):

<script type="text/javascript">
(function() {
window.seriesDTO = {
  "series" : [ {
    "id" : "YOU",
    "data" : [ {
      "startDate" : "Fri, 30 Aug 2013 00:00:00",
      "endDate" : "Mon, 23 Sep 2013 23:59:59",
      "value" : 66,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Sep 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 30 Aug 2013 00:00:00",
      "upperDate" : "Mon, 23 Sep 2013 23:59:59"
    }, {
      "startDate" : "Tue, 24 Sep 2013 00:00:00",
      "endDate" : "Tue, 22 Oct 2013 23:59:59",
      "value" : 85,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Oct 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 24 Sep 2013 00:00:00",
      "upperDate" : "Tue, 22 Oct 2013 23:59:59"
    }, {
      "startDate" : "Wed, 23 Oct 2013 00:00:00",
      "endDate" : "Thu, 21 Nov 2013 23:59:59",
      "value" : 135,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Fri, 01 Nov 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 23 Oct 2013 00:00:00",
      "upperDate" : "Thu, 21 Nov 2013 23:59:59"
    }, {
      "startDate" : "Fri, 22 Nov 2013 00:00:00",
      "endDate" : "Mon, 23 Dec 2013 23:59:59",
      "value" : 134,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Dec 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 22 Nov 2013 00:00:00",
      "upperDate" : "Mon, 23 Dec 2013 23:59:59"
    }, {
      "startDate" : "Tue, 24 Dec 2013 00:00:00",
      "endDate" : "Thu, 23 Jan 2014 23:59:59",
      "value" : 72,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Wed, 01 Jan 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 24 Dec 2013 00:00:00",
      "upperDate" : "Thu, 23 Jan 2014 23:59:59"
    }, {
      "startDate" : "Fri, 24 Jan 2014 00:00:00",
      "endDate" : "Mon, 24 Feb 2014 23:59:59",
      "value" : 109,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sat, 01 Feb 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 24 Jan 2014 00:00:00",
      "upperDate" : "Mon, 24 Feb 2014 23:59:59"
    }, {
      "startDate" : "Tue, 25 Feb 2014 00:00:00",
      "endDate" : "Tue, 25 Mar 2014 23:59:59",
      "value" : 84,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sat, 01 Mar 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 25 Feb 2014 00:00:00",
      "upperDate" : "Tue, 25 Mar 2014 23:59:59"
    }, {
      "startDate" : "Wed, 26 Mar 2014 00:00:00",
      "endDate" : "Thu, 24 Apr 2014 23:59:59",
      "value" : 86,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Apr 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 26 Mar 2014 00:00:00",
      "upperDate" : "Thu, 24 Apr 2014 23:59:59"
    }, {
      "startDate" : "Fri, 25 Apr 2014 00:00:00",
      "endDate" : "Fri, 23 May 2014 23:59:59",
      "value" : 56,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Thu, 01 May 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 25 Apr 2014 00:00:00",
      "upperDate" : "Fri, 23 May 2014 23:59:59"
    }, {
      "startDate" : "Sat, 24 May 2014 00:00:00",
      "endDate" : "Tue, 24 Jun 2014 23:59:59",
      "value" : 76,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Jun 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Sat, 24 May 2014 00:00:00",
      "upperDate" : "Tue, 24 Jun 2014 23:59:59"
    }, {
      "startDate" : "Wed, 25 Jun 2014 00:00:00",
      "endDate" : "Thu, 24 Jul 2014 23:59:59",
      "value" : 72,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Jul 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 25 Jun 2014 00:00:00",
      "upperDate" : "Thu, 24 Jul 2014 23:59:59"
    }, {
      "startDate" : "Fri, 25 Jul 2014 00:00:00",
      "endDate" : "Mon, 25 Aug 2014 23:59:59",
      "value" : 81,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Fri, 01 Aug 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 25 Jul 2014 00:00:00",
      "upperDate" : "Mon, 25 Aug 2014 23:59:59"
    } ],
    "unit" : "KWH",
    "precision" : 0
  }, {
    "id" : "NEIGHBORS",
    "data" : [ {
      "startDate" : "Fri, 30 Aug 2013 00:00:00",
      "endDate" : "Mon, 23 Sep 2013 23:59:59",
      "value" : 257.958526611328125,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Sep 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 30 Aug 2013 00:00:00",
      "upperDate" : "Mon, 23 Sep 2013 23:59:59"
    }, {
      "startDate" : "Tue, 24 Sep 2013 00:00:00",
      "endDate" : "Tue, 22 Oct 2013 23:59:59",
      "value" : 316.710968017578125,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Oct 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 24 Sep 2013 00:00:00",
      "upperDate" : "Tue, 22 Oct 2013 23:59:59"
    }, {
      "startDate" : "Wed, 23 Oct 2013 00:00:00",
      "endDate" : "Thu, 21 Nov 2013 23:59:59",
      "value" : 373.00909423828125,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Fri, 01 Nov 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 23 Oct 2013 00:00:00",
      "upperDate" : "Thu, 21 Nov 2013 23:59:59"
    }, {
      "startDate" : "Fri, 22 Nov 2013 00:00:00",
      "endDate" : "Mon, 23 Dec 2013 23:59:59",
      "value" : 442.25927734375,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Dec 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 22 Nov 2013 00:00:00",
      "upperDate" : "Mon, 23 Dec 2013 23:59:59"
    }, {
      "startDate" : "Tue, 24 Dec 2013 00:00:00",
      "endDate" : "Thu, 23 Jan 2014 23:59:59",
      "value" : 389.46453857421875,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Wed, 01 Jan 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 24 Dec 2013 00:00:00",
      "upperDate" : "Thu, 23 Jan 2014 23:59:59"
    }, {
      "startDate" : "Fri, 24 Jan 2014 00:00:00",
      "endDate" : "Mon, 24 Feb 2014 23:59:59",
      "value" : 402.319488525390625,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sat, 01 Feb 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 24 Jan 2014 00:00:00",
      "upperDate" : "Mon, 24 Feb 2014 23:59:59"
    }, {
      "startDate" : "Tue, 25 Feb 2014 00:00:00",
      "endDate" : "Tue, 25 Mar 2014 23:59:59",
      "value" : 319.855438232421875,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sat, 01 Mar 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 25 Feb 2014 00:00:00",
      "upperDate" : "Tue, 25 Mar 2014 23:59:59"
    }, {
      "startDate" : "Wed, 26 Mar 2014 00:00:00",
      "endDate" : "Thu, 24 Apr 2014 23:59:59",
      "value" : 328.7225341796875,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Apr 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 26 Mar 2014 00:00:00",
      "upperDate" : "Thu, 24 Apr 2014 23:59:59"
    }, {
      "startDate" : "Fri, 25 Apr 2014 00:00:00",
      "endDate" : "Fri, 23 May 2014 23:59:59",
      "value" : 301.809600830078125,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Thu, 01 May 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 25 Apr 2014 00:00:00",
      "upperDate" : "Fri, 23 May 2014 23:59:59"
    }, {
      "startDate" : "Sat, 24 May 2014 00:00:00",
      "endDate" : "Tue, 24 Jun 2014 23:59:59",
      "value" : 324.0477294921875,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Jun 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Sat, 24 May 2014 00:00:00",
      "upperDate" : "Tue, 24 Jun 2014 23:59:59"
    }, {
      "startDate" : "Wed, 25 Jun 2014 00:00:00",
      "endDate" : "Thu, 24 Jul 2014 23:59:59",
      "value" : 291.14556884765625,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Jul 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 25 Jun 2014 00:00:00",
      "upperDate" : "Thu, 24 Jul 2014 23:59:59"
    }, {
      "startDate" : "Fri, 25 Jul 2014 00:00:00",
      "endDate" : "Mon, 25 Aug 2014 23:59:59",
      "value" : 326.228668212890625,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Fri, 01 Aug 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 25 Jul 2014 00:00:00",
      "upperDate" : "Mon, 25 Aug 2014 23:59:59"
    } ],
    "unit" : "KWH",
    "precision" : 0
  }, {
    "id" : "EFFICIENT_NEIGHBORS",
    "data" : [ {
      "startDate" : "Fri, 30 Aug 2013 00:00:00",
      "endDate" : "Mon, 23 Sep 2013 23:59:59",
      "value" : 87.5,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Sep 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 30 Aug 2013 00:00:00",
      "upperDate" : "Mon, 23 Sep 2013 23:59:59"
    }, {
      "startDate" : "Tue, 24 Sep 2013 00:00:00",
      "endDate" : "Tue, 22 Oct 2013 23:59:59",
      "value" : 112.07543182373046875,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Oct 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 24 Sep 2013 00:00:00",
      "upperDate" : "Tue, 22 Oct 2013 23:59:59"
    }, {
      "startDate" : "Wed, 23 Oct 2013 00:00:00",
      "endDate" : "Thu, 21 Nov 2013 23:59:59",
      "value" : 123,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Fri, 01 Nov 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 23 Oct 2013 00:00:00",
      "upperDate" : "Thu, 21 Nov 2013 23:59:59"
    }, {
      "startDate" : "Fri, 22 Nov 2013 00:00:00",
      "endDate" : "Mon, 23 Dec 2013 23:59:59",
      "value" : 143,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Dec 2013 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 22 Nov 2013 00:00:00",
      "upperDate" : "Mon, 23 Dec 2013 23:59:59"
    }, {
      "startDate" : "Tue, 24 Dec 2013 00:00:00",
      "endDate" : "Thu, 23 Jan 2014 23:59:59",
      "value" : 116,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Wed, 01 Jan 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 24 Dec 2013 00:00:00",
      "upperDate" : "Thu, 23 Jan 2014 23:59:59"
    }, {
      "startDate" : "Fri, 24 Jan 2014 00:00:00",
      "endDate" : "Mon, 24 Feb 2014 23:59:59",
      "value" : 131,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sat, 01 Feb 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 24 Jan 2014 00:00:00",
      "upperDate" : "Mon, 24 Feb 2014 23:59:59"
    }, {
      "startDate" : "Tue, 25 Feb 2014 00:00:00",
      "endDate" : "Tue, 25 Mar 2014 23:59:59",
      "value" : 109,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sat, 01 Mar 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Tue, 25 Feb 2014 00:00:00",
      "upperDate" : "Tue, 25 Mar 2014 23:59:59"
    }, {
      "startDate" : "Wed, 26 Mar 2014 00:00:00",
      "endDate" : "Thu, 24 Apr 2014 23:59:59",
      "value" : 105,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Apr 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 26 Mar 2014 00:00:00",
      "upperDate" : "Thu, 24 Apr 2014 23:59:59"
    }, {
      "startDate" : "Fri, 25 Apr 2014 00:00:00",
      "endDate" : "Fri, 23 May 2014 23:59:59",
      "value" : 103,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Thu, 01 May 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 25 Apr 2014 00:00:00",
      "upperDate" : "Fri, 23 May 2014 23:59:59"
    }, {
      "startDate" : "Sat, 24 May 2014 00:00:00",
      "endDate" : "Tue, 24 Jun 2014 23:59:59",
      "value" : 119,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Sun, 01 Jun 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Sat, 24 May 2014 00:00:00",
      "upperDate" : "Tue, 24 Jun 2014 23:59:59"
    }, {
      "startDate" : "Wed, 25 Jun 2014 00:00:00",
      "endDate" : "Thu, 24 Jul 2014 23:59:59",
      "value" : 102,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Tue, 01 Jul 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Wed, 25 Jun 2014 00:00:00",
      "upperDate" : "Thu, 24 Jul 2014 23:59:59"
    }, {
      "startDate" : "Fri, 25 Jul 2014 00:00:00",
      "endDate" : "Mon, 25 Aug 2014 23:59:59",
      "value" : 104,
      "estimated" : false,
      "shouldUseMajorityMonthForBilling" : true,
      "startOfMajorityMonth" : "Fri, 01 Aug 2014 00:00:00",
      "firstMajorityMonth" : null,
      "lastMajorityMonth" : null,
      "lowerDate" : "Fri, 25 Jul 2014 00:00:00",
      "upperDate" : "Mon, 25 Aug 2014 23:59:59"
    } ],
    "unit" : "KWH",
    "precision" : 0
  }, {
    "id" : "BILL_IDENTIFIERS",
    "data" : [ {
      "value" : 1,
      "year" : 2013,
      "startDate" : "Fri, 30 Aug 2013 00:00:00",
      "endDate" : "Mon, 23 Sep 2013 23:59:59",
      "lowerDate" : "Fri, 30 Aug 2013 00:00:00",
      "upperDate" : "Mon, 23 Sep 2013 23:59:59"
    }, {
      "value" : 2,
      "year" : 2013,
      "startDate" : "Tue, 24 Sep 2013 00:00:00",
      "endDate" : "Tue, 22 Oct 2013 23:59:59",
      "lowerDate" : "Tue, 24 Sep 2013 00:00:00",
      "upperDate" : "Tue, 22 Oct 2013 23:59:59"
    }, {
      "value" : 3,
      "year" : 2013,
      "startDate" : "Wed, 23 Oct 2013 00:00:00",
      "endDate" : "Thu, 21 Nov 2013 23:59:59",
      "lowerDate" : "Wed, 23 Oct 2013 00:00:00",
      "upperDate" : "Thu, 21 Nov 2013 23:59:59"
    }, {
      "value" : 4,
      "year" : 2013,
      "startDate" : "Fri, 22 Nov 2013 00:00:00",
      "endDate" : "Mon, 23 Dec 2013 23:59:59",
      "lowerDate" : "Fri, 22 Nov 2013 00:00:00",
      "upperDate" : "Mon, 23 Dec 2013 23:59:59"
    }, {
      "value" : 1,
      "year" : 2014,
      "startDate" : "Tue, 24 Dec 2013 00:00:00",
      "endDate" : "Thu, 23 Jan 2014 23:59:59",
      "lowerDate" : "Tue, 24 Dec 2013 00:00:00",
      "upperDate" : "Thu, 23 Jan 2014 23:59:59"
    }, {
      "value" : 2,
      "year" : 2014,
      "startDate" : "Fri, 24 Jan 2014 00:00:00",
      "endDate" : "Mon, 24 Feb 2014 23:59:59",
      "lowerDate" : "Fri, 24 Jan 2014 00:00:00",
      "upperDate" : "Mon, 24 Feb 2014 23:59:59"
    }, {
      "value" : 3,
      "year" : 2014,
      "startDate" : "Tue, 25 Feb 2014 00:00:00",
      "endDate" : "Tue, 25 Mar 2014 23:59:59",
      "lowerDate" : "Tue, 25 Feb 2014 00:00:00",
      "upperDate" : "Tue, 25 Mar 2014 23:59:59"
    }, {
      "value" : 4,
      "year" : 2014,
      "startDate" : "Wed, 26 Mar 2014 00:00:00",
      "endDate" : "Thu, 24 Apr 2014 23:59:59",
      "lowerDate" : "Wed, 26 Mar 2014 00:00:00",
      "upperDate" : "Thu, 24 Apr 2014 23:59:59"
    }, {
      "value" : 5,
      "year" : 2014,
      "startDate" : "Fri, 25 Apr 2014 00:00:00",
      "endDate" : "Fri, 23 May 2014 23:59:59",
      "lowerDate" : "Fri, 25 Apr 2014 00:00:00",
      "upperDate" : "Fri, 23 May 2014 23:59:59"
    }, {
      "value" : 6,
      "year" : 2014,
      "startDate" : "Sat, 24 May 2014 00:00:00",
      "endDate" : "Tue, 24 Jun 2014 23:59:59",
      "lowerDate" : "Sat, 24 May 2014 00:00:00",
      "upperDate" : "Tue, 24 Jun 2014 23:59:59"
    }, {
      "value" : 7,
      "year" : 2014,
      "startDate" : "Wed, 25 Jun 2014 00:00:00",
      "endDate" : "Thu, 24 Jul 2014 23:59:59",
      "lowerDate" : "Wed, 25 Jun 2014 00:00:00",
      "upperDate" : "Thu, 24 Jul 2014 23:59:59"
    }, {
      "value" : 8,
      "year" : 2014,
      "startDate" : "Fri, 25 Jul 2014 00:00:00",
      "endDate" : "Mon, 25 Aug 2014 23:59:59",
      "lowerDate" : "Fri, 25 Jul 2014 00:00:00",
      "upperDate" : "Mon, 25 Aug 2014 23:59:59"
    } ]
  } ],
  "sufficientData" : true
};
})();
</script>

You can parse the javascript code with slimit package: 您可以使用slimit包解析javascript代码:

SlimIt is a JavaScript minifier written in Python. SlimIt是用Python编写的JavaScript压缩程序。 It compiles JavaScript into more compact code so that it downloads and runs faster. 它将JavaScript编译为更紧凑的代码,以便下载和运行更快。

SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor. SlimIt还提供了一个库,其中包括JavaScript解析器,词法分析器,漂亮的打印机和树访问器。

In your case, you can apply it by finding the first assignment to an object: 对于您的情况,可以通过找到对象的第一个分配来应用它:

import json

from bs4 import BeautifulSoup
from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor


data = """
<script type="text/javascript">
(function() {
window.seriesDTO = {
...
};
})();
</script>
"""

soup = BeautifulSoup(data)

parser = Parser()
tree = parser.parse(soup.script.text)

json_data = next(node.right.to_ecma() for node in nodevisitor.visit(tree)
                 if isinstance(node, ast.Assign) and isinstance(node.right, ast.Object))

print json.loads(json_data)

Prints a Python dictionary: 打印Python字典:

{u'series': [{u'data': [{u'endDate': u'Mon, 23 Sep 2013 23:59:59',
                         u'estimated': False,
                         u'firstMajorityMonth': None,
                         u'lastMajorityMonth': None,
                         u'lowerDate': u'Fri, 30 Aug 2013 00:00:00',
                         u'shouldUseMajorityMonthForBilling': True,
                         u'startDate': u'Fri, 30 Aug 2013 00:00:00',
                         u'startOfMajorityMonth': u'Sun, 01 Sep 2013 00:00:00',
                         u'upperDate': u'Mon, 23 Sep 2013 23:59:59',
                         u'value': 66},
...
{u'endDate': u'Mon, 25 Aug 2014 23:59:59',
                         u'lowerDate': u'Fri, 25 Jul 2014 00:00:00',
                         u'startDate': u'Fri, 25 Jul 2014 00:00:00',
                         u'upperDate': u'Mon, 25 Aug 2014 23:59:59',
                         u'value': 8,
                         u'year': 2014}],
              u'id': u'BILL_IDENTIFIERS'}],
 u'sufficientData': True}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用c3.js在javascript中制作图形 - how can i make a graph in javascript using c3.js 如何从服务器实时获取数据以更新Chartjs图形? - How can i get data in real time from the server to update my chartjs graph? 关于 mxGraph:如何先添加 Edge,然后添加自己的自定义属性,然后添加到图形中? - About mxGraph: how can I first add Edge, and add my own custom attribute, then add to the graph? 如何让我的 javascript 函数在 head 标签内工作? - How can I make my javascript functions work inside the head tag? 如何从本地存储中获取特定数据,以便在 JavaScript 中以模态显示它们? - How do I get specific data from my localstorage so I can show them in a modal in JavaScript? 我的个人资料图片通过Javascript SDK开放图谱api的参数返回。 如何获得以.JPG结尾的直接网址? - My profile picture is coming back with parameters with Javascript SDK open graph api. How can I get a straight url that ends in .JPG? 如何制作我的锚标签(此) - How can I make my anchor tag (this) 如何在图表内和 hover 上显示百分比? - How can I display the percentage inside the graph and on hover? 如何使svg圆始终显示在图形线路径上方? - How can I make an svg circle always appear above my graph line path? 我怎样才能让我的Jchartfx面积图显示在我的gridview下? - How can i get my Jchartfx area graph to show up underneath my gridview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM