【发布时间】:2017-01-23 18:55:18
【问题描述】:
<div class="jokeContent">
<h2 style="color:#369;">Can I be Frank</h2>
What did Ellen Degeneres say to Kathy Lee?
<p></p> <p>Can I be Frank with you? </p>
<p>Submitted by Calamjo</p>
<p>Edited by Curtis</p>
<div align="right" style="margin-top:10px;margin-bottom:10px;">#joke <a href="http://www.jokesoftheday.net/tag/short-jokes/">#short</a> </div>
<div style="clear:both;"></div>
</div>
所以我试图提取 之后和 [div aign = "right" style=...] 节点之前的所有文本。 到目前为止我所尝试的:
jokes = response.xpath('//div[@class="jokeContent"]')
for joke in jokes:
text = joke.xpath('text()[normalize-space()]').extract()]
if len(text) > 0:
yield text
这在某种程度上有效,但网站在 html 中不一致,有时文本嵌入在 <.p> TEXT 中,有时嵌入在 <.br> TEXT 中或只是 TEXT。 所以我认为只提取标题之后和样式节点之前的所有内容可能是有意义的,然后可以在后面进行过滤。
【问题讨论】:
标签: xpath web-scraping scrapy