zoukankan      html  css  js  c++  java
  • PYTHON PYQUERY 基本用法

    引用方法

    from pyquery import PyQuery as pq
    View Code

    基本CSS选择器

    from pyquery import PyQuery as pq
    html = '''
        <div id="wrap">
            <ul class="s_from">
                asdasd
                <link href="http://asda.com">asdadasdad12312</link>
                <link href="http://asda1.com">asdadasdad12312</link>
                <link href="http://asda2.com">asdadasdad12312</link>
            </ul>
        </div>
    '''
    doc = pq(html)
    print doc("#wrap .s_from link")
    View Code

    运行结果

    <link href="http://asda.com">asdadasdad12312</link>
                <link href="http://asda1.com">asdadasdad12312</link>
                <link href="http://asda2.com">asdadasdad12312</link>
    View Code

    #是查找id的标签  .是查找class 的标签  link 是查找link 标签 中间的空格表示里层

    遍历查找结果

    from pyquery import PyQuery as pq
    html = '''
        <div href="wrap">
            hello nihao
            <ul class="s_from">
                asdasd
                <link class='active1 a123' href="http://asda.com">asdadasdad12312</link>
                <link class='active2' href="http://asda1.com">asdadasdad12312</link>
                <link class='movie1' href="http://asda2.com">asdadasdad12312</link>
            </ul>
        </div>
    '''
    
    doc = pq(html)
    its=doc("link").items()
    for it in its:
        print(it)
    View Code

    运行结果

    <link class="active1 a123" href="http://asda.com">asdadasdad12312</link>
                
    <link class="active2" href="http://asda1.com">asdadasdad12312</link>
                
    <link class="movie1" href="http://asda2.com">asdadasdad12312</link>
    View Code

    获取属性信息

    from pyquery import PyQuery as pq
    html = '''
        <div href="wrap">
            hello nihao
            <ul class="s_from">
                asdasd
                <link class='active1 a123' href="http://asda.com">asdadasdad12312</link>
                <link class='active2' href="http://asda1.com">asdadasdad12312</link>
                <link class='movie1' href="http://asda2.com">asdadasdad12312</link>
            </ul>
        </div>
    '''
    
    doc = pq(html)
    its=doc("link").items()
    for it in its:
        print(it.attr('href'))
        print(it.attr.href)
    View Code

      运行结果

    http://asda.com
    http://asda.com
    http://asda1.com
    http://asda1.com
    http://asda2.com
    http://asda2.com
    View Code

     获取文本

    from pyquery import PyQuery as pq
    html = '''
        <div href="wrap">
            hello nihao
            <ul class="s_from">
                asdasd
                <link class='active1 a123' href="http://asda.com">asdadasdad12312</link>
                <link class='active2' href="http://asda1.com">asdadasdad12312</link>
                <link class='movie1' href="http://asda2.com">asdadasdad12312</link>
            </ul>
        </div>
    '''
    
    doc = pq(html)
    its=doc("link").items()
    for it in its:
        print(it.text())
    View Code

     运行结果

    asdadasdad12312
    asdadasdad12312
    asdadasdad12312
    View Code
  • 相关阅读:
    用人之道(一) 如何组建软件开发队伍[转]
    用人之道(二) 何管理软件开发团队[转]
    2005年度世界500强公司名单[转]
    人类的15个欲望与游戏设计[转&收藏]
    Flash读取Cookie[转]
    高效程序员应该养成的七个习惯
    六度隔离学说,1967年,哈佛大学,Stanley Milgram
    做技术,切不可沉湎于技术[转&收藏]
    庆祝VSX团队成立,加入VSX团队申请帖
    如何把菜单添加到另外一个VSPackage的菜单里?
  • 原文地址:https://www.cnblogs.com/xlsxls/p/9724715.html
Copyright © 2011-2022 走看看