如何提取所有源代码并导出为html?<table> </table>

时间:2015-02-15 17:17:11

标签: python html scrapy scrapy-spider

我是Scrapy的初学者。我的目标是从大HTML页面中提取所选表格,然后以HTML格式将所选表格导出。基本上,我想要的是获取原始网页的较短版本,只保留<table>部分

每个<table>部分的结构如下所示:

<table>
   <tbody>
      <tr>
        <td> 
          <font>

目前,我正在尝试以下蜘蛛代码,但问题是:

  1. 它不会保留所有源格式;
  2. <table></table>不包括在内;
  3. 我不知道如何以HTML格式保存已删除的结果。

  4.   def parse(self, response):
          hxs = HtmlXPathSelector(response)
          titles = hxs.select("//document/type/sequence/filename/description/text/table")
          items = []
          for titles in titles:
              item = MyHtmlItem()
              item ["htmltext"] = titles.select("node()").extract()
              if (item["htmltext"]):
                  items.append(item)
          return items
    

    有人能给我一些建议吗?

1 个答案:

答案 0 :(得分:1)

如果我理解正确并且您只需要从页面中提取原始表格html,那么解决方案非常简单:

def parse(self, response):
    # XPath query to get all tables from response
    tables_selectors = response.xpath('//table')
    tables_html = tables_selectors.extract()
    ...

tables_html是来自原始表html的字符串数组。根据需要进行处理。

一些建议:

您的语法看起来有点过时,似乎您使用的是过时的Scrapy手册。

请查看official site

上的最新文档

使用表时,请注意XPath查询中的tbody标记。

希望这有帮助!