【问题标题】:Scrape HTML table with standard Python HTMLParser使用标准 Python HTMLParser 抓取 HTML 表
【发布时间】:2017-06-08 16:37:19
【问题描述】:

我希望仅使用标准 Python HTML 解析器从 HTML 表中抓取数据。我需要坚持使用库存工具,因为代码将广泛分布,我无法花时间支持那些需要安装 BeautifulSoup、lxml 等的人。

例如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

因为在 HTML 表格之外还有一些“a”标签。是否有人可以使用标准 HTML 解析方法从表中提取数据或 href 值?

【问题讨论】:

  • 想通了。需要在解析器中创建一个计数器以记录解析器何时从 HTTP 表中获取信息。还使用 if 语句处理异常(例如目录和其他不需要的 href)。

标签: python html html-parsing


【解决方案1】:
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() 

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2012-04-12
    • 2017-11-09
    • 2016-01-31
    • 1970-01-01
    • 2019-10-30
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多