Python中PyQuery库的使用总结
来源:互联网 发布:如何用java写网页 编辑:程序博客网 时间:2024/05/08 01:19
Python中PyQuery库的使用总结
pyquery库是jQuery的Python实现,可以用于解析HTML网页内容。
from pyquery import PyQuery as pqprint('----1.可加载一段HTML字符串,或一个HTML文件,或是一个url地址----')d1 = pq('<html><title>hello</title></html>')d2 = pq(url='http://movie.douban.com/subject/1309069/')#d3 = pq(filename='result\\02-tieba\\16pn=1.html')print('----2.html()和text() ——获取相应的HTML块或文本块----')d1 = pq('<html><title>hello</title></html>')print(d1('html')) # <html><title>hello</title></html>print(d1('html').html()) # <title>hello</title>print(d1('html').text()) # helloprint('----3.根据HTML标签来获取元素----')d=pq('<div><p>test 1</p><p>test 2</p></div>')print(d('p')) # <p>test 1</p><p>test 2</p>print(d('p').html()) # test 1print('----4.eq(index) ——根据给定的索引号得到指定元素----')d=pq('<div><p>test 1</p><p>test 2</p></div>')print(d('p').html())print(d('p').eq(0).html()) # test 1print(d('p').eq(1).html()) # test 2print(d('p').eq(2).html()) # Noneprint('----5.filter() ——根据类名、id名得到指定元素----')d=pq("<div><p id='one'>test 1</p><p class='two'>test 2</p></div>")print(d('p').filter('#one')) # <p id="one">test 1</p>print(d('p').filter('.two')) # <p class="two">test 2</p>print('----6.find() ——查找嵌套元素----')d=pq("<div><p id='one'>test 1</p><p class='two'>test 2</p></div>")print(d('div').find('p')) # <p id="one">test 1</p><p class="two">test 2</p>print(d('div').find('p').eq(0)) # <p id="one">test 1</p>print('----7.直接根据类名、id名获取元素----')d=pq("<div><p id='one'>test 1</p><p class='two'>test 2</p></div>")print(d('#one').html()) # test 1print(d('.two').html()) # test 2print('----8.获取属性值----')d=pq("<p id='my_id'><a href='http://hello.com'>hello</a></p>")print(d('a').attr('href')) # http://hello.comprint(d('p').attr('id')) # my_idprint('----9.修改属性值----')d=pq("<p id='my_id'><a href='http://hello.com'>hello</a></p>")print(d) # <p id="my_id"><a href="http://hello.com">hello</a></p>d('a').attr('href','http://baidu.com')print(d) # <p id="my_id"><a href="http://baidu.com">hello</a></p>print('----10.addClass(value) ——为元素添加类----')d=pq('<div></div>')print(d) # <div/>d.addClass('my_class')print(d) # <div class="my_class"/>print('----11.hasClass(name) #返回判断元素是否包含给定的类----')d=pq("<div class='my_class'></div>")print(d.hasClass('my_class')) # Trueprint(d.hasClass('my_c')) # Falseprint('----12.children(selector=None) ——获取子元素----')d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")print(d.children()) # <p id="one">hello</p><p id="two">world</p>print(d.children('#two')) # <p id="two">world</p>print('----13.parents(selector=None)——获取父元素----')d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")print(d('p').parents()) # <span><p id="one">hello</p><p id="two">world</p></span>print(d('#one').parents('span'))# <span><p id="one">hello</p><p id="two">world</p></span>print(d('#one').parents('p')) # []print('----14.clone() ——返回一个节点的拷贝----')d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")print(d('#one')) # <p id="one">hello</p>print(d('#one').clone()) # <p id="one">hello</p>print('----15.empty() ——移除节点内容----')d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")print(d) # <span><p id="one">hello</p><p id="two">world</p></span>d('#one').empty()print(d) # <span><p id="one"/><p id="two">world</p></span>print('----16.nextAll(selector=None) ——返回后面全部的元素块----')d=pq("<p id='one'>hello</p><p id='two'>world</p><img scr='' />")print(d('p:first').nextAll()) # <p id="two">world</p><img scr=""/>print(d('p:last').nextAll()) # <img scr=""/>print('----17.not_(selector) ——返回不匹配选择器的元素----')d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")print(d('p').not_('#two')) # <p id="one">hello</p>'''爬取豆瓣电影页面中主演'''if __name__ == '__main__': print('----爬取豆瓣电影页面中主演----') # 读取Batman Begins页面 doc = pq(url='http://movie.douban.com/subject/3077412/') # 遍历starring节点 starring = doc("a[rel='v:starring']") # 转化为Map stars = starring.map(lambda i,e:pq(e).text()) print('<<%s>>的主演:' % (doc("span[property='v:itemreviewed']").text())) for i in stars: print(i)'''执行结果:----爬取豆瓣电影页面中主演----<<寻龙诀>>的主演:陈坤黄渤舒淇杨颖夏雨刘晓庆颜卓灵曹操张东黄西僧格仁钦'''
GitHub 地址:https://github.com/Jueee/05-WebCrawlers/blob/master/22-PyQuery.py
0 0
- Python中PyQuery库的使用总结
- Python中PyQuery库的使用总结
- Python中PyQuery库的使用总结
- Python中PyQuery库的使用总结
- Python中PyQuery库的使用总结
- Python中PyQuery库的使用总结
- python使用pyquery库总结
- Python学习笔记—PyQuery库的使用总结
- Python学习笔记—PyQuery库的使用总结
- python之pyQuery使用总结
- PyQuery库的使用
- python爬虫由浅入深10---pyquery库的基础与使用
- PyQuery: 一个类似jQuery的Python库
- Python PyQuery的用法
- python:使用pyquery分析html
- PyQuery库使用
- 利用python中的pyquery库简单的抓取数据
- 使用pyquery碰到的问题
- eclipse maven搭建
- php线程安全与非线程安全安装包选择
- OpenCV 3.10+Visual Studio 2013+Camera
- 几个不熟悉的标准C的基础库
- cookie相关的函数
- Python中PyQuery库的使用总结
- NYOJ A problem is easy(水题)
- cocoapods pod install 安装报错 is not used in any concrete target
- 整型、实型及符号型常量
- C++学习笔记(一)
- 使用DML语句更改数
- HTML中锚点及href=#与href=javascriptvoid(0)的区别
- mysql_新手FAQ
- Ubuntu安装vncserver实现图形化访问