• pyquery CSS选择器子元素


    子元素
    from pyquery import PyQuery as pq
    doc = pq(html)
    items = doc('.list')
    print('items 类型',type(items))
    print(items)
    lis = items.find('li')
    print('lis 类型',type(lis))
    print(lis)
    '''
    输出结果:items 类型 <class 'pyquery.pyquery.PyQuery'>
    <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     
    lis 类型 <class 'pyquery.pyquery.PyQuery'>
    <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
    '''
    print('-------------------')
    lis = items.children()
    print(type(lis))
    print(lis)
    
    '''
    输出结果为:
    -------------------
    <class 'pyquery.pyquery.PyQuery'>
    <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         
    '''
    print('****************')
    lis = items.parents()
    print(type(lis))
    print(lis)
    
    '''
    输出结果为:
    ****************
    <class 'pyquery.pyquery.PyQuery'>
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
    
    
    '''
    #筛选出li里面有active的所有子元素
    lis = items.children('.active')
    print(lis)
    
    #输出结果
    <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             
    

      

  • 相关阅读:
    css实现强制不换行/自动换行/强制换行
    提供一个跨浏览器的XML DOM对象解决方案,来自于《javascript高级程序设计》
    Javascript的IE和Firefox兼容性汇编收藏.txt
    弹出一个层来让用户确认操作(转)
    弹窗代码
    C# 绘制统计图(柱状图, 折线图, 扇形图) (转)
    把Sql类型转换为C#类型的函数
    类型对象和类实例对象
    javascript 父窗体获取子窗体操作结果
    类型转换(二):是使用 is 还是 as
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/10272782.html
Copyright © 2020-2023  润新知