我是Scrapy的初学者。我的目标是从大HTML页面中提取所选表格,然后以HTML格式将所选表格导出。基本上,我想要的是获取原始网页的较短版本,只保留<table>
部分。
每个<table>
部分的结构如下所示:
<table>
<tbody>
<tr>
<td>
<font>
目前,我正在尝试以下蜘蛛代码,但问题是:
<table>
和</table>
不包括在内; 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
有人能给我一些建议吗?
答案 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手册。
上的最新文档使用表时,请注意XPath查询中的tbody标记。
希望这有帮助!