CSS选择器以获取元素属性值 [英] CSS Selector to get the element attribute value
本文介绍了CSS选择器以获取元素属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML结构如下:
<td class='hey'>
<a href="https://example.com">First one</a>
</td>
这是我的选择器:
m_URL = sel.css("td.hey a:nth-child(1)[href] ").extract()
我的选择器现在将输出< a href ="https://example.com">第一个</a>
,但我只希望它输出链接本身:<代码> https://example.com .
My selector now will output <a href="https://example.com">First one</a>
, but I only want it to output the link itself: https://example.com
.
我该怎么做?
推荐答案
从 a
标记获取 :: attr(value)
.
演示(使用 Scrapy shell ):
$ scrapy shell index.html
>>> response.css('td.hey a:nth-child(1)::attr(href)').extract()
[u'https://example.com']
其中 index.html
包含:
<table>
<tr>
<td class='hey'>
<a href="https://example.com">Fist one</a>
</td>
</tr>
</table>
这篇关于CSS选择器以获取元素属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文