推荐学习书目
› 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 · 1928 views
    This topic created in 3402 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   ·   Privacy   ·   Solana   ·   875 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:22 · PVG 07:22 · LAX 16:22 · JFK 19:22
    ♥ Do have faith in what you're doing.