CSS Selector to get the element attribute value

此生再无相见时 提交于 2021-02-06 10:50:45

问题


The HTML structure is like this:

<td class='hey'> 
<a href="https://example.com">First one</a>
</td>

This is my selector:

m_URL = sel.css("td.hey a:nth-child(1)[href] ").extract()  

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.

How can I do that?


回答1:


Get the ::attr(value) from the a tag.

Demo (using Scrapy shell):

$ scrapy shell index.html
>>> response.css('td.hey a:nth-child(1)::attr(href)').extract()
[u'https://example.com']

where index.html contains:

<table>
    <tr>
        <td class='hey'>
            <a href="https://example.com">Fist one</a>
        </td>
    </tr>
</table>



回答2:


you may try this:

m_URL = sel.css("td.hey a:nth-child(1)").xpath('@href').extract()


来源:https://stackoverflow.com/questions/24987480/css-selector-to-get-the-element-attribute-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!