2017-01-24 49 views
0

我正在尋找僅使用標準Python HTML解析器從HTML表格中刮取數據。我需要堅持與股票的工具,因爲該代碼將被廣泛分發,我不能花時間去支持那些誰需要安裝BeautifulSoup,lxlml等使用標準Python HTMLParser刮取HTML表格

例如,HTML代碼:

<table id="indexlist"> 
 
    <tbody> 
 
    <tr class="indexhead"> 
 
     <th class="indexcolicon"> 
 
     <img src="/icons/blank.gif" alt="[ICO]"> 
 
     </th> 
 
     <th class="indexcolname"> 
 
     <a href="?C=N;O=D">Name 
 
     </a> 
 
     </th> 
 
     <th class="indexcollastmod"> 
 
     <a href="?C=M;O=A">Last modified 
 
     </a> 
 
     </th> 
 
     <th class="indexcolsize"> 
 
     <a href="?C=S;O=A">Size 
 
     </a> 
 
     </th> 
 
    </tr> 
 
    <tr class="parent"> 
 
    <td class="indexcolicon"> 
 
     <a href="/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/browse/"> 
 
     <img src="/icons/back.gif" alt="[PARENTDIR]"> 
 
     </a> 
 
    </td> 
 
    <td class="indexcolname"> 
 
     <a href="/pub/DATASETS/nsidc0081_nrt_nasateam_seaice/browse/">Parent Directory 
 
     </a> 
 
    </td> 
 
    <td class="indexcollastmod">&nbsp; 
 
    </td> 
 
    <td class="indexcolsize"> - 
 
    </td> 
 
    </tr> 
 
    <tr class="odd"> 
 
    <td class="indexcolicon"> 
 
     <a href="nt_20150101_f17_nrt_n.png"> 
 
     <img src="/icons/image2.gif" alt="[IMG]"> 
 
     </a> 
 
    </td> 
 
    <td class="indexcolname"> 
 
     <a href="nt_20150101_f17_nrt_n.png"> 
 
     nt_20150101_f17_nrt_n.png 
 
     </a> 
 
    </td> 
 
    <td class="indexcollastmod"> 
 
     2015-03-10 11:25 
 
    </td> 
 
    <td class="indexcolsize"> 56K 
 
    </td> 
 
    </tr> 
 
    <tr class="even"> 
 
    <td class="indexcolicon"> 
 
    <a href="nt_20150102_f17_nrt_n.png"> 
 
     <img src="/icons/image2.gif" alt="[IMG]"> 
 
     </a> 
 
    </td> 
 
    <td class="indexcolname"> 
 
     <a href="nt_20150102_f17_nrt_n.png"> 
 
     nt_20150102_f17_nrt_n.png 
 
     </a> 
 
    </td> 
 
. 
 
. 
 
.

我希望能夠提取此表中的'數據'。更具體地說,數據將是以* .png結尾的所有屬性值。它們與表格中的數據共享相同的名稱。我不想明確聲明我想刮* .png文件,因爲我想在不同的目錄中使用這些代碼,這些目錄將具有不同的文件格式。我嘗試了一些試圖提取名稱爲'href'的所有屬性值的代碼,但是這也返回了HTML正文中的許多其他屬性。僅刮除數據還會返回一些不在表格之外的實例。例如:

class MyHTMLParser(HTMLParser): 
def __init__(self): 
    HTMLParser.__init__(self) 
    self.inLink = False 
    self.dataArray = [] 

def handle_starttag(self, tag, attrs): 
    self.inLink = False 
    if tag == 'a': 
     for name, value in attrs: 
      if name == 'href': 
       self.inLink = True 
       self.lasttag = tag 

def handle_data(self, data): 
    if self.lasttag == 'a' and self.inLink and data.strip(): 
     self.dataArray.append(data) 

然而,這個返回如下:

nt_20170119_f18_nrt_n.png 
    nt_20170120_f18_nrt_n.png 
    nt_20170121_f18_nrt_n.png 
    nt_20170122_f18_nrt_n.png 
    Home 
    | 
    Contact Us 

,因爲也有幾個 'A' 標記,生活在HTML表格外。是否有人使用標準HTML解析方法從表格中提取數據或href值?

+0

想通了。需要在解析器中創建一個計數器,以便在解析器從HTTP表中提供信息時進行記錄。還用if語句處理異常(例如目錄和其他不需要的hrefs)。 – GDeezy

回答

0
class MyHTMLParser(HTMLParser): 
def __init__(self): 
    HTMLParser.__init__(self) 
    self.inLink = False 
    self.dataList = [] 
    self.directory = '/' 
    self.indexcol = ';' 
    self.Counter = 0 

def handle_starttag(self, tag, attrs): 
    self.inLink = False 
    if tag == 'table': 
     self.Counter += 1 
    if tag == 'a': 
     for name, value in attrs: 
      if name == 'href': 
       if self.directory in value or self.indexcol in value: 
        break 
       else: 
        self.inLink = True 
        self.lasttag = tag 

def handle_endtag(self, tag): 
     if tag == 'table': 
      self.Counter +=1 

def handle_data(self, data): 
    if self.Counter == 1: 
     if self.lasttag == 'a' and self.inLink and data.strip(): 
      self.dataList.append(data) 

parser = MyHTMLParser()