【发布时间】:2015-01-17 06:28:17
【问题描述】:
假设我有很多这样的 html 脚本:
<div style="clear:both" id="novelintro" itemprop="description">you are foolish!<font color=red size=4>I am superman!</font></div>
我想用xpath来提取文字:你太傻了!我是超人!
但是,如果我使用
xpath('//div[@id="novelintro"]/text()').extract()
我只能得到“你太傻了!”
当我使用时:
xpath('//div[@id="novelintro"]/font/text()').extract()"
我只能得到“我是超人!”
所以如果你只能使用一个 xpath 表达式来提取整个句子“你是愚蠢的!我是超人!”
而且比较倒霉,在上面的html脚本中,是“<font>”标签,但是在我的其他脚本中,还有很多其他标签,例如:
提取“嗨,女孩,我爱你!”在以下脚本中:
<div style="clear:both" id="novelintro" itemprop="description">hi girl<legend >I love you!</legend></div>
提取“如果我嫁给你的母亲,那么我就是你的父亲!”在以下脚本中:
<div style="clear:both" id="novelintro" itemprop="description">If I<legend > marry your mother<div>then I am your father!</div></legend></div>
如果你可以只使用一个 xpath 表达式来适配所有的 html 脚本?
【问题讨论】:
标签: python html xml xpath scrapy