V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jun 8, 2017 · 1688 views
    This topic created in 3255 days ago, the information mentioned may be changed or developed.

    当前的 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  
       Jun 8, 2017   ❤️ 1
    xpath //tr/@class
    af463419014
        2
    af463419014  
    OP
       Jun 8, 2017
    @Morriaty 非常感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:48 · PVG 03:48 · LAX 12:48 · JFK 15:48
    ♥ Do have faith in what you're doing.