V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
af463419014
V2EX  ›  Python

scrapy 获取元素的 class 属性

  •  
  •   af463419014 · 2017-06-08 17:03:39 +08:00 · 1284 次点击
    这是一个创建于 2515 天前的主题,其中的信息可能已经有所发展或是发生改变。

    当前的 selector 已找到元素 a 比如

    <tr class="class1">
      <td class="class2"></td>
      <td class="class3"></td>
    </tr>
    

    现在希望输出 tr 这个元素的 class,也就是'class1' 但是如果使用 a.css('::attr(class)'),那么结果是['class1','class2','class3']

    如果使用 a.css('::attr(class)')[0],直接输出第一个,那么当元素为下面这样时 结果是'class2',但是实际上需要的结果是 null

    <tr>
      <td class="class2"></td>
      <td class="class3"></td>
    </tr>
    

    请问怎样才能保证得到 tr 元素 class 的值

    Morriaty
        1
    Morriaty  
       2017-06-08 17:08:04 +08:00   ❤️ 1
    xpath //tr/@class
    af463419014
        2
    af463419014  
    OP
       2017-06-08 17:13:13 +08:00
    @Morriaty 非常感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5455 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:49 · PVG 15:49 · LAX 00:49 · JFK 03:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.