PYTHON PYQUERY 基本用法

2021-06-16 19:03

阅读:434

标签:选择器   hello   图片   引用   splay   none   opened   items   import   

引用方法

技术分享图片技术分享图片
from pyquery import PyQuery as pq
View Code

基本CSS选择器

技术分享图片技术分享图片
from pyquery import PyQuery as pq
html = ‘‘‘
    
    asdasd asdadasdad12312 asdadasdad12312 asdadasdad12312
‘‘‘ doc = pq(html) print doc("#wrap .s_from link")
View Code

运行结果

技术分享图片技术分享图片
"http://asda.com">asdadasdad12312
            "http://asda1.com">asdadasdad12312
            "http://asda2.com">asdadasdad12312
View Code

#是查找id的标签  .是查找class 的标签  link 是查找link 标签 中间的空格表示里层

遍历查找结果

技术分享图片技术分享图片
from pyquery import PyQuery as pq
html = ‘‘‘
    
hello nihao
    asdasd asdadasdad12312 asdadasdad12312 asdadasdad12312
‘‘‘ doc = pq(html) its=doc("link").items() for it in its: print(it)
View Code

运行结果

技术分享图片技术分享图片
class="active1 a123" href="http://asda.com">asdadasdad12312
            
class="active2" href="http://asda1.com">asdadasdad12312
            
class="movie1" href="http://asda2.com">asdadasdad12312
View Code

获取属性信息

技术分享图片技术分享图片
from pyquery import PyQuery as pq
html = ‘‘‘
    
hello nihao
    asdasd asdadasdad12312 asdadasdad12312 asdadasdad12312
‘‘‘ doc = pq(html) its=doc("link").items() for it in its: print(it.attr(href)) print(it.attr.href)
View Code

  运行结果

技术分享图片技术分享图片
http://asda.com
http://asda.com
http://asda1.com
http://asda1.com
http://asda2.com
http://asda2.com
View Code

 获取文本

技术分享图片技术分享图片
from pyquery import PyQuery as pq
html = ‘‘‘
    
hello nihao
    asdasd asdadasdad12312 asdadasdad12312 asdadasdad12312
‘‘‘ doc = pq(html) its=doc("link").items() for it in its: print(it.text())
View Code

 运行结果

技术分享图片技术分享图片
asdadasdad12312
asdadasdad12312
asdadasdad12312
View Code

 

PYTHON PYQUERY 基本用法

标签:选择器   hello   图片   引用   splay   none   opened   items   import   

原文地址:https://www.cnblogs.com/xlsxls/p/9724715.html


评论


亲,登录后才可以留言!