【问题标题】:css selector nth-of-type not workcss选择器第n个类型不起作用
【发布时间】:2014-09-25 08:47:58
【问题描述】:

我想知道如何用css选择器选择www.test.com?p=2

我用next_page = sel.css(u"div.page_sp > a.pagecl:nth-of-type(2)::attr(href)")

www.test.com?p=2

但是没用
请指导我

结构如下:

<div class="page_sp">
    <a class="pagecl" href="">prevpage</a> 
    <a class="page" href="">1</a> 
    <a class="page" href="">2</a> 
    ....
    <a class="page" href="">9</a> 
    <a class="page" href="">10</a> 
    <a class="pagecl" href="www.test.com?p=2">nextpage</a> 
    <span class="page_sp">1/20 </span>
</div>

【问题讨论】:

    标签: css-selectors scrapy


    【解决方案1】:

    nth-of-type 不适用于 class 名称,请参阅 css3 nth of type restricted to class

    相反,您可以使用nextpage 文本搜索a

    >>> response.css("div.page_sp > a:contains('nextpage')::attr(href)").extract()
    [u'www.test.com?p=2']
    

    【讨论】:

      【解决方案2】:

      非常糟糕的代码!!! 您只是想获取下一页链接吗? 使用这个:

      $("a:last-of-type").attr("href");
      

      虽然,我必须说,有更好的方法可以做到这一点,你应该使用其他选择器。但是好吧,你没有给我们任何上下文,所以就在这里!!!

      【讨论】:

        猜你喜欢
        • 2012-12-31
        • 2016-08-02
        • 2015-06-05
        • 2019-01-17
        • 2011-03-11
        • 2023-03-25
        • 1970-01-01
        • 2017-06-04
        • 1970-01-01
        相关资源
        最近更新 更多