PYTHON 爬虫笔记六:PyQuery库基础用法

知识点一:PyQuery库详解及其基本使用

  • 初始化

  1. 字符串初始化

    html = '''
    
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
     
    print(doc('li'))#选择器实际上就是CSS选择器,即:选id就加“#”,选class前面加“.”
  2. class="item-0">first item
  3. class="item-1">"link2.html">second item<>/li
  4. class="item-0 active">"link3.html">class="boid">third item
  5. class="item-1 active">"link4.html">fourth item
  6. class="item-0">"link5.html">fifth item
  7. 获得的结果
  8. URL初始化

    from pyquery import PyQuery as pq
    doc1 = pq(url = "http://www.baidu.com")
    
    print(doc1("head"))
    "content-type" content="text/html;charset=utf-8"/>"X-UA-Compatible" content="IE=Edge"/>"always" name="referrer"/>"stylesheet" type="text/css" href="http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css"/>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é 
    获得的结果
  9. 文件初始化

    from pyquery import PyQuery as pq
    doc2 = pq(filename = "demo.html")#自己下载一个HTML文件
     
    print(doc2('li'))
  • 基本CSS选择器

  1. 实例

    tml = '''
    
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    
    print(doc("#container .list li"))#注意空格,空格代表嵌套关系
  2. class="item-0">first item
  3. class="item-1">"link2.html">second item<>/li
  4. class="item-0 active">"link3.html">class="boid">third item
  5. class="item-1 active">"link4.html">fourth item
  6. class="item-0">"link5.html">fifth item
  7. 获得的结果
  8. 查询元素

    1. 子元素

      html = '''
      
      '''
      from pyquery import PyQuery as pq
      doc = pq(html)
      items = doc(".list")#首先选中url标签
      
      print(type(items))
      print(items)
       
      lis = items.find('li')#实际上也是一个CSS选择器,将里面所有的li标签都打印出来;只要在它里面的标签都可以找到
      print(type(lis))
      print(lis)
      
      #查找直接子元素
      lis2 = items.children()
      print(type(lis2))
      print(lis2)
       
      lis3 = items.children('.active')
      print(lis3)
      <class 'pyquery.pyquery.PyQuery'>
      
      
      <class 'pyquery.pyquery.PyQuery'>
      
    2. class="item-0">first item
    3. class="item-1">"link2.html">second item<>/li
    4. class="item-0 active">"link3.html">class="boid">third item
    5. class="item-1 active">"link4.html">fourth item
    6. class="item-0">"link5.html">fifth item
    7. <class 'pyquery.pyquery.PyQuery'>
    8. class="item-0">first item
    9. class="item-1">"link2.html">second item<>/li
    10. class="item-0 active">"link3.html">class="boid">third item
    11. class="item-1 active">"link4.html">fourth item
    12. class="item-0">"link5.html">fifth item
    13. class="item-0 active">"link3.html">class="boid">third item
    14. class="item-1 active">"link4.html">fourth item
    15. 获得的结果
    16. 父元素

      #父元素
      html = '''
      
      '''
      from pyquery import PyQuery as pq
      doc = pq(html)
       
      items = doc(".list")#首先选中url标签
      #每个标签外面肯定只能套一个父元素
      container = items.parent()
      
      print(type(container))
      print(container)
      <class 'pyquery.pyquery.PyQuery'>
      
      "container">
      获得的结果

      另一种方法:

      html = '''
      
         
                 
      ''' from pyquery import PyQuery as pq doc = pq(html) items = doc(".list")#首先选中url标签 #将所有祖先节点返回 parents = items.parents() print(parents) print(type(parents))#打印出两个div
      另一种方法
      class="wrap">
      "container">
      class="wrap">
      "container">
      class="wrap">
      "container">
      "container">
      <class 'pyquery.pyquery.PyQuery'>
      --->获得的结果
      html = '''
      
         
                 
      ''' from pyquery import PyQuery as pq doc = pq(html) items = doc(".list")#首先选中url标签 #在其中进行搜索 parents1 = items.parents(".wrap") print(parents1)#通过筛选,只剩下一个div
      获取单一内容
      class="wrap">
      "container">
      --->获得的结果
    17. 兄弟元素

      html = '''
      
         
                 
      ''' from pyquery import PyQuery as pq doc = pq(html) li = doc('.list .item-0.active')#首先选class=“.list”,空格即使选择list里面的标签,再选class=“item-0”,并列active(实际就是一个整体) print(li) print(li.siblings())#获取所有的兄弟元素
    18. class="item-0 active">"link3.html">class="boid">third item
    19. class="item-1">"link2.html">second item<>/li
    20. class="item-0">first item
    21. class="item-1 active">"link4.html">fourth item
    22. class="item-0">"link5.html">fifth item
    23. 获得的结果

      另一种方式:

      html = '''
      
         
                 
      ''' from pyquery import PyQuery as pq doc = pq(html) li = doc('.list .item-0.active')#首先选class=“.list”,空格即使选择list里面的标签,再选class=“item-0”,并列active(实际就是一个整体) #在向其中筛选 print(li.siblings('.active'))
      另一种方式
    24. class="item-1 active">"link4.html">fourth item
    25. --->获得的结果
  •  遍历

  1. 单个元素

    html = '''
    
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
     
    li = doc(".item-0.active")
    print(li)
  2. class="item-0 active">"link3.html">class="boid">third item
  3. 获得的方法

    另一种方式

    html = '''
    
    '''
    from pyquery import PyQuery as pq 
    doc = pq(html)
    
    lis = doc('li').items()#多个元素,进行遍历,生成一个产生器
    
    print(type(lis))
    for li in lis:
        print(li)
    另一种方式
    <class 'generator'>
    
  4. class="item-0">first item
  5. class="item-1">"link2.html">second item
  6. class="item-0 active">"link3.html">class="boid">third item
  7. class="item-1 active">"link4.html">fourth item
  8. class="item-0">"link5.html">fifth item
  9. --->获得的结果
  • 获取信息

  1. 获取属性

    html = '''
    
    '''
     
    from pyquery import PyQuery as pq
    doc = pq(html)
    a = doc(".item-0.active a")#选择class同时为item-0和active,在选择class里面的啊标签,中间注意空格
    print(a)
    print(a.attr("href"))
    print(a.attr.href)#结果同上
    "link3.html">class="boid">third item
    link3.html
    link3.html
    获得的结果
  2. 获取文本

    html = '''
    
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    a = doc(".item-0.active a")
    
    print(a)
    print(a.text())#将上面的选中的class中包围的文字
    获得的结果
  3. 获取HTML

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

  1. address,removeClass

    html = '''
    
    '''
    from pyquery import PyQuery as pq
    doc = pq(html)
    
    li = doc(".item-0.active")
    print(li)
    
    li.removeClass("active")#移除active
    print(li)
    
    li.addClass("active")#增加active
    print(li)
    获得的结果
  2. 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")#增加style属性
    print(li)
  3. class="item-0 active">"link3.html">class="boid">third item
  4. class="item-0 active" name="link">"link3.html">class="boid">third item
  5. class="item-0 active" name="link" style="font-size: 14px">"link3.html">class="boid">third item
  6. 获得的结果
  7. remove

    html1 = '''
    
    Hello,World

    This is a paragraph.

    ''' from pyquery import PyQuery as pq doc = pq(html1) wrap = doc(".wrap") print(wrap.text()) wrap.find('p').remove() print(wrap.text())
    Hello,World
    This is a paragraph.
    Hello,World
    获得的结果
  8. 其他DOM操作

    1. 其他DOM方法: http://pythonhosted.org/pyquery/

 

转载于:https://www.cnblogs.com/darwinli/p/9447201.html

你可能感兴趣的:(PYTHON 爬虫笔记六:PyQuery库基础用法)