作家
登录

Python爬虫神器PyQuery的使用方法

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

reddit'
  •  
  • print p.prepend('Oh yes!'
  •  
  • d = pq('<div class="wrap"><div id="test"><a href=http://developer.51cto.com/art/201705/"http://cuiqingcai.com">Germy
  • '
  •  
  • p.prependTo(d('#test')) 
  •  
  • print p 
  •  
  • print d 
  •  
  • d.empty() 
  •  
  • print d  
  • 运行结不雅

    1. <p id="hello" class="hello"check out <a href=http://developer.51cto.com/art/201705/"http://reddit.com/r/python"><span>reddit</span></a></p> 
    2.  
    3. <p id="hello" class="hello">Oh yes! check out <a href=http://developer.51cto.com/art/201705/"http://reddit.com/r/python"><span>reddit</span></a></p> 
    4.  
    5. <p id="hello" class="hello">Oh yes! check out <a href=http://developer.51cto.com/art/201705/"http://reddit.com/r/python"><span>reddit</span></a></p> 
    6.  
    7. <div class="wrap"><div id="test"><p id="hello" class="hello">Oh yes! check out <a href=http://developer.51cto.com/art/201705/"http://reddit.com/r/python"><span>reddit</span></a></p><a href=http://developer.51cto.com/art/201705/"http://cuiqingcai.com">Germy</a></div></div> 
    8.  
    9. <div class="wrap"/>  

    这不须要多说清楚明了吧。

    DOM 操作也是与 jQuery 千篇一律。

    遍历用到 items 办法返回对象列表,或者用 lambda

    1. from pyquery import PyQuery as pq 
    2.  
    3. doc = pq(filename='hello.html'
    4.  
    5. lis = doc('li'
    6.  
    7. for li in lis.items(): 
    8.  
    9.     print li.html() 
    10.  
    11.   
    12.  
    13. print lis.each(lambda e: e)  

    运行结不雅

    1. first item 
    2.  

    3.   推荐阅读

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

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


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

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

    顶一下

    关键词: 探索发现

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

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)
    科技快报
    品牌展示