Scrapy:如何一起提取超链接文本

时间:2017-06-14 20:37:24

标签: python scrapy web-crawler

我正在抓教授的webpage

根据她的研究描述,有两个超链接,分别是“TEDx UCL”和“here”。

我使用像<li ng-repeat="rule in property | filter: filterAdded"></li> 这样的xpath 得到前3段。

'//div[@class="group"]//p/text()'获取包含一些换行符的最后一段。但这些可以很容易地清理。

问题是最后一段只包含文字。超链接丢失。虽然我可以单独提取它们,但将它们放回相应的位置是很繁琐的。

如何获取所有文本并保留超链接?

2 个答案:

答案 0 :(得分:0)

您可以使用html2text

sample = response.xpath("//div[@class="group"]//p/text()")
converter = html2text.HTML2Text()
converter.ignore_links = True
converter.handle(sample)

答案 1 :(得分:0)

试试这个:

'//div[@class="group"]/p//text()[normalize-space(.)]'