【问题标题】:Web Scraping - Access elements using css selector in pythonWeb Scraping - 在 python 中使用 css 选择器访问元素
【发布时间】:2022-01-14 08:26:08
【问题描述】:

我是 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()

【问题讨论】:

  • 您能否提供有关您正在使用的代码/库的更多详细信息?
  • 我正在使用scrapy

标签: python web-scraping scrapy


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 2012-08-16
    • 2011-12-28
    • 2022-11-04
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多