【发布时间】:2016-05-07 04:25:23
【问题描述】:
我想深入了解一些统计数据,并编写一些代码。
import urllib2
import HTMLParser
response = urllib2.urlopen('http://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=MCESTUS1&f=M')
data = response.read()
class TableParser(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.in_td = False
def handle_starttag(self, tag, attrs):
if tag == 'td':
self.in_td = True
def handle_data(self, data):
if self.in_td:
print data
def handle_endtag(self, tag):
self.in_td = False
p = TableParser()
raw_data = p.feed(data)
raw_list = []
for string in raw_data:
raw_list.append(string)
print raw_list
此脚本的一些输出/
2015
421,472
448,039
474,815
483,379
479,335
469,539
455,470
457,810
460,786
486,700
-
Release Date: 12/31/2015
Next Release Date: 1/29/2016
Traceback (most recent call last):
File "FirstData.py", line 28, in <module>
for string in raw_data:
TypeError: 'NoneType' object is not iterable
这行得通,但我不能先遍历 Nonetype 对象。
其次,我如何将这些数据放入 Pandas 中以获取带有月份和数量的图表?
【问题讨论】: