06-pyquery基础

PyQuery

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

初始化

字符串初始化

html = """

"""

from pyquery import PyQuery as pq
doc = pq(html)
print(doc('li'))
  • first item
  • second item
  • third item
  • fourth item
  • fifth item
  • URL 初始化

    from pyquery import PyQuery as pq
    doc = pq(url='http://www.baidu.com')
    print(doc('head'))
    print(type(doc('head')))
    百度一下,你就知道 
    
    

    文件初始化

    from pyquery import PyQuery as pq
    doc = pa(filename='xxx')
    print(doc('li'))

    基本CSS选择器

    如果是类 .
    如果是id #
    如果是标签 就什么也不加
    说白了 和CSS 选择器用法一样 也可以使用后代选择器...
    
    html = """
    
    """
    
    from pyquery import PyQuery as pq
    doc = pq(html)
    print(doc('#container .list li'))
  • first item
  • second item
  • third item
  • fourth item
  • fifth item
  • 查找元素

    子元素

    html = """
    
    """
    
    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)
    
    
    
    
    
  • first item
  • second item
  • third item
  • fourth item
  • fifth item
  • lis = items.children()
    print(type(lis))
    print(lis)
    
    
  • first item
  • second item
  • third item
  • fourth item
  • fifth item
  • lis = items.children('.active')
    print(lis)
  • third item
  • fourth item
  • 父元素

    html = """
    
    """
    
    from pyquery import PyQuery as pq
    doc = pq(html)
    items = doc('.list')
    container = items.parent()
    print(type(container))
    print(container)
    
    
    
    # 查找所有的祖先元素
    
    html = """
    
    """
    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 = """
    
    """
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.list .item-0.active')
    print(li.siblings())
  • second item
  • first item
  • fourth item
  • fifth item
  • print(li.siblings('.active'))
  • fourth item
  • 遍历

    单个元素

    html = """
    
    """
    
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.item-0.active')
    print(li)
  • third item
  • html = """
    
    """
    from pyquery import PyQuery as pq
    doc = pq(html)
    lis = doc('li').items()
    print(lis)
    print(type(lis))
    for li in lis:
        print(li.find('a'))
    
    
    
    second item
    third item
    fourth item
    fifth item
    

    获取信息

    获取属性

    通过attr(‘属性名’) 或 attr.属性名

    html = """
    
    """
    
    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)
    third item
    link3.html
    link3.html
    

    获取文本

    通过 .text()方法获取文本内容

    html = """
    
    """
    
    from pyquery import PyQuery as pq
    doc = pq(html)
    a = doc('.item-0.active a')
    print(a)
    print(a.text())
    third item
    third item
    

    获取HTML

    通过.html()方式获取HTML代码

    html = """
    
    """
    
    from pyquery import PyQuery as pq
    doc = pq(html)
    a = doc('.item-0.active')
    print(a)
    print(a.html())
  • third item
  • third item

    DOM操作

    addClass、 removeClass

    html = """
    
    """
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('.item-0.active')
    print(li)
    li.removeClass('active')
    print(li)
    li.addClass('acitve')
    print(li)
  • third item
  • third item
  • third item
  • attr 、css

    html = """
    
    """
    
    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)
  • third item
  • third item
  • third item
  • remove

    html = """
    
    Hello,World

    This is a paragrap

    """
    from pyquery import PyQuery as pq doc = pq(html) wrap = doc('.wrap') print(wrap.text()) wrap.find('p').remove() print(wrap.text())
    Hello,World
    This is a paragrap
    Hello,World
    

    伪类选择器

    html = """
    
    """
    
    from pyquery import PyQuery as pq
    doc = pq(html)
    li = doc('li:first-child')
    print(li)
    li = doc('li:last-child')
    print(li)
    li = doc('li:nth-child(2)')
    print(li)
    li = doc('li:gt(2)')
    print(li)
    li = doc('li:nth-child(2n)')
    print(li)
    li = doc('li:contains(second)')
    print(li)
  • first item
  • fifth item
  • second item
  • fourth item
  • fifth item
  • second item
  • fourth item
  • second item
  • 你可能感兴趣的:(python_爬虫,Python3爬虫之路)