到目前为止,使用python库 twill 和 beautifulsoup 我能够登录我公用事业公司的网站,并使用我的燃气和电力使用数据进入页面分别。他们实际上用javascript创建了一个很好的情节,原始数据可以隐藏在那里。
twill 库使用showforms()
,show()
等非常好地显示Web表单#1,#2,#3等的组合,但不是javascript元素据我所知。 beautifulsoup 似乎能够显示html元素,比如网页的标题,例如:title1 = soup1.html.head.title
,但我似乎无法列出javascript元素(有4个像或页面上的5个不同的javascripts,其中一个包含下面的数据。
是否有办法分别在 twill 或 beautifulsoup 中使用showjavascript()
或soup1.html.body.javascript
这样的内容来访问此javascript数据并解析它导入 matplotlib 或其他python绘图包?
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>
答案 0 :(得分:1)
您可以使用slimit
包解析javascript代码:
SlimIt是一个用Python编写的JavaScript缩小器。它汇编 将JavaScript转换为更紧凑的代码,以便下载和运行 更快。
SlimIt还提供了一个包含JavaScript解析器的库, lexer,漂亮的打印机和树访客。
在您的情况下,您可以通过查找对象的第一个分配来应用它:
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)
打印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}