zoukankan      html  css  js  c++  java
  • python PyQuery

    强大又灵活的网页解析库。如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的绝佳选择

    安装PyQuery

    pip3 install pyquery

    初始化

    字符串初始化
    html = '''
    <div>
        <ul>
            <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    print(doc('li'))
    
    URL初始化
    from pyquery import PyQuery as pq
    doc = pq(url='http://www.baidu.com')
    print(doc('head'))
    
    文件初始化
    from pyquery import PyQuery as pq
    doc = pq(filename='demo.html')
    print(doc('li'))
    
    基本CSS选择器
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    print(doc('#container .list li'))
    

    查找元素

    子元素
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    items =doc('.list')
    print(type(items))
    print(items)
    lis = items.find('li')
    print(type(lis))
    print(lis)
    
    lis = items.children()
    print(type(lis))
    print(lis)
    
    lis = items.children('.active')
    print(lis)
    
    父元素
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    items =doc('.list')
    container = items.parent()
    print(type(container))
    print(container)
    
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    items =doc('.list')
    parents = items.parents()
    print(type(parents))
    print(parents)
    
    parent = items.parents('.wrap')
    print(parent)
    
    兄弟元素
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li =doc('.list .item-0.active')
    print(li.siblings())
    
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li =doc('.list .item-0.active')
    print(li.siblings('.active'))
    

    遍历

    单个元素
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.item-0.active')
    print(li)
    
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    lis = doc('li').items()
    print(type(lis))
    for li in lis:
        print(li)
    

    获取信息

    获取属性
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    a = doc('.item-0.active a')
    print(a)
    print(a.attr('href'))
    print(a.attr.href)
    
    获取文本
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    a = doc('.item-0.active a')
    print(a)
    print(a.text())
    
    获取HTML
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.item-0.active')
    print(li)
    print(li.html())
    

    DOM操作

    addClass、removeClass
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.item-0.active')
    print(li)
    li.removeClass('active')
    print(li)
    li.addClass('active')
    print(li)
    
    attr、css
    html = '''
    <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>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.item-0.active')
    print(li)
    li.attr('name','link')
    print(li)
    li.css('font-size','14px')
    print(li)
    
    remove
    html = '''
    <div class="wrap">
    Hello World
    <p>This is aparagraph.</p>
    <div>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    wrap = doc('.wrap')
    print(wrap.text())
    wrap.find('p').remove()
    print(wrap.text())
    

    伪类选择器

    html = '''
    <div class="wrap">
        <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>
    </div>
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('li:first-child')
    print(li)
    li = doc('li:last-child')
    print(li)
    
    大道理谁都懂,鸡汤也听过,可我们为什么还是过不好这一生。
  • 相关阅读:
    峰Spring4学习(1)HelloWorld
    小峰mybatis(5)mybatis使用注解配置sql映射器--动态sql
    前端实现某一列不能重复不能且不能为空
    jquery:给正则表达式添加变量
    css:width height
    让heigh:100%起作用
    jquery:选择器 过滤器
    vs:如何添加.dll文件
    jq:正则表达式
    css:html() text() val()
  • 原文地址:https://www.cnblogs.com/puqunzhu/p/9811271.html
Copyright © 2011-2022 走看看