【问题标题】:Iterate Nonetype object迭代无类型对象
【发布时间】: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 中以获取带有月份和数量的图表?

【问题讨论】:

    标签: python object nonetype


    【解决方案1】:

    看起来解析器已解析,但您需要通过其他方法从解析器中获取数据,而不是p.feed(data) 的返回值,因为它始终是None。将它累积到作为解析器对象属性的列表中怎么样:

    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
            self.raw_data = []
    
        def handle_starttag(self, tag, attrs):
            if tag == 'td':
                self.in_td = True
    
        def handle_data(self, data):
            if self.in_td:
                print data
                self.raw_data.append(data)
    
        def handle_endtag(self, tag):
            self.in_td = False
    
    p = TableParser()
    p.feed(data)
    
    print p.raw_data
    

    未经测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-28
      • 2017-02-23
      • 2015-06-07
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多