Home  >  Q&A  >  body text

python - pyspider 如何获取某个列表中的第n个元素

response.doc('.link_nei>ul>li>a').items()
上面的语句是用来获取所有符合条件的 <a>,我想获得倒数第二个 <a> 应该怎么获得? 教程上都是通过正则表达式去筛选,不过这种方法好 low 而且不实用

黄舟黄舟2741 days ago544

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:33:15

    response.doc('.link_nei>ul>li>a').eq(-2).items()
    

    Refer to the pyquery documentation https://pythonhosted.org/pyqu...

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:33:15

    I see you are using a CSS selector, just add :nth-last-child(2) after a.

    CSS selector tutorial: http://www.w3school.com.cn/cs...

    reply
    0
  • Cancelreply