Web剪贴-在python中使用css选择器访问元素



我是python网络抓取的初学者,我一直在做这件事:如何使用css选择器访问这个div标记中的标题文本?

<div class="p13n-sc-truncated selectorgadget_suggested" aria-hidden="true" data-rows="1" title="Ikigai: The Japanese secret to a long and happy life">Ikigai: The Japanese secret to a long and… </div>

我尝试了以下代码,但没有工作:

title = response.css('.p13n-sc-truncated::attr[title]').extract()

您使用了方括号而不是圆括号:

In [1]: response.css('.p13n-sc-truncated::attr(title)').extract()
Out[1]: ['Ikigai: The Japanese secret to a long and happy life']

最新更新