我怎样才能在python的scrapy选择器中只提取文本 [英] How can i extract only text in scrapy selector in python

查看:24
本文介绍了我怎样才能在python的scrapy选择器中只提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

   site = hxs.select("//h1[@class='state']")
   log.msg(str(site[0].extract()),level=log.ERROR)

输出是

 [scrapy] ERROR: <h1 class="state"><strong>
            1</strong>
            <span> job containing <strong>php</strong> in <strong>region</strong> paying  <strong>$30-40k per year</strong></span>
                </h1>

是否可以只获取没有任何html标签的文本

Is it possible to only get the text without any html tags

推荐答案

//h1[@class='state']

在上面的 xpath 中,您选择具有 class 属性 state

in your above xpath you are selecting h1 tag that has class attribute state

所以这就是它选择 h1 元素

如果你只想选择 h1 标签的文本,你所要做的就是

if you just want to select text of h1 tag all you have to do is

//h1[@class='state']/text()

如果你想选择h1标签及其子标签的文本,你必须使用

if you want to select text of h1 tag as well as its children tags, you have to use

//h1[@class='state']//text()

所以区别在于 /text() 用于特定标签文本和 //text() 用于特定标签及其子标签的文本

so the difference is /text() for specific tag text and //text() for text of specific tag as well as its children tags

下面提到的代码适合你

site = ''.join(hxs.select("//h1[@class='state']/text()").extract()).strip()

这篇关于我怎样才能在python的scrapy选择器中只提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆