作家
登录

Python爬虫神器PyQuery的使用方法

作者: 来源: 2017-05-31 09:47:11 阅读 我要评论

as pq 
  •  
  •   
  •  
  • p = pq('<p id="hello" class="hello"></p>')('p'
  •  
  • print p.attr("id"
  •  
  • print p.attr("id""plop"
  •  
  • print p.attr("id""hello" 
  • 运行结不雅

    1. hello 
    2.  
    3. <p id="plop" class="hello"/> 
    4.  
    5. <p id="hello" class="hello"/>  

    再来一发

    1. from pyquery import PyQuery as pq 
    2.  
    3.   
    4.  
    5. p = pq('<p id="hello" class="hello"></p>')('p'
    6.  
    7. print p.addClass('beauty'
    8.  
    9. print p.removeClass('hello'
    10.  
    11. print p.css('font-size''16px'
    12.  
    13. print p.css({'background-color''yellow'})  

    运行结不雅

    1. <p id="hello" class="hello beauty"/> 
    2.  
    3. <p id="hello" class="beauty"/> 
    4.  
    5. <p id="hello" class="beauty" style="font-size: 16px"/> 
    6.  
    7. <p id="hello" class="beauty" style="font-size: 16px; background-color: yellow"/>  

    依旧是那媚暌高古与自负!

    在这里我们发清楚明了,这是连续串的操作,而 p 是一向在本来的结不雅上变更的。

    是以履行上述操作之后,p 本身也产生了变更。

    DOM操作

    遍历

    同样的原汁原味的 jQuery 语法

    1. from pyquery import PyQuery as pq 
    2.  
    3.   
    4.  
    5. p = pq('<p id="hello" class="hello"></p>')('p'
    6.  
    7. print p.append(' check out <a href=http://developer.51cto.com/art/201705/"http://reddit.com/r/python">

        推荐阅读

        OTT IPTV商机广阔 运营商如何进一步发掘CDN机会窗口

      在此前4月份的亚太CDN峰会上,中国联通收集技巧研究院、高等工程师乔治曾做过一个猜测:全球流量复合增长率占>>>详细阅读


      本文标题:Python爬虫神器PyQuery的使用方法

      地址:http://www.17bianji.com/lsqh/35490.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)