Python HTMLParser打印出空白行

时间:2015-08-28 01:40:02

标签: python html-parsing

我正在使用python的HTMLParser并且遇到打印出空行的问题。

from HTMLParser import HTMLParser
import urllib2
class MyHTMLParser(HTMLParser):
def handle_data(self, data):
     print "Encountered some data  :", data

# instantiate the parser and fed it some HTML
url = 'http://www.ngccoin.com/price-guide/us/flying-eagle-cents-pscid-16-desig-ms'
req = urllib2.Request(url, headers={'User-Agent' :"Magic Browser"})
response = urllib2.urlopen(req)
html = response.read()

parser = MyHTMLParser()
parser.feed( html )

我的问题是当它到达数据部分时,它只打印出新行和实际数据。我的输出看起来很像:

Encountered some data  :

Encountered some data  : Official Grading Service of
Encountered some data  :

Encountered some data  :

Encountered some data  :

我应该如何让它忽略那些只有新线的线?

0 个答案:

没有答案