• 你是否觉得 XPath 的用法多少有点晦涩难记呢?

    你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢?

    你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢?

    你是否已经有了一些前端基础了解选择器却与另外一些奇怪的选择器语法混淆了呢?

    嗯,那么,前端大大们的福音来了,PyQuery 来了,乍听名字,你一定联想到了 jQuery,如果你对 jQuery 熟悉,那么 PyQuery 来解析文档就是不二之选!包括我在内!

    PyQuery 是 Python 仿照 jQuery 的严格实现。语法与 jQuery 几乎完全相同,所以不用再去费心去记一些奇怪的方法了。

    天下竟然有这等好事?我都等不及了!

    安装

    有这等神器还不赶紧安装了!来!

     
    pip install pyquery

    还是原来的配方,还是熟悉的味道。

    参考来源

    本文内容参考官方文档,更多内容,大家可以去官方文档学习,毕竟那里才是最原汁原味的。

    目前版本 1.2.4 (2016/3/24)

    官方文档

    简介

    pyquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.

    This is not (or at least not yet) a library to produce or interact with javascript code. I just liked the jquery API and I missed it in python so I told myself “Hey let’s make jquery in python”. This is the result.

    It can be used for many purposes, one idea that I might try in the future is to use it for templating with pure http templates that you modify using pyquery. I can also be used for web scrapping or for theming applications with Deliverance.

    pyquery 可让你用 jQuery 的语法来对 xml 进行操作。这I和 jQuery 十分类似。如果利用 lxml,pyquery 对 xml 和 html 的处理将更快。

    这个库不是(至少还不是)一个可以和 JavaScript交互的代码库,它只是非常像 jQuery API 而已。

    初始化

    在这里介绍四种初始化方式。

    (1)直接字符串

    from pyquery import PyQuery as pq
    doc = pq("<html></html>")
     

    pq 参数可以直接传入 HTML 代码,doc 现在就相当于 jQuery 里面的 $ 符号了。

    (2)lxml.etree

    from lxml import etree
    doc = pq(etree.fromstring("<html></html>"))
     

    可以首先用 lxml 的 etree 处理一下代码,这样如果你的 HTML 代码出现一些不完整或者疏漏,都会自动转化为完整清晰结构的 HTML代码。

    (3)直接传URL

    from pyquery import PyQuery as pq
    doc = pq('http://www.baidu.com')

    这里就像直接请求了一个网页一样,类似用 urllib2 来直接请求这个链接,得到 HTML 代码。

    (4)传文件

    from pyquery import PyQuery as pq
    doc = pq(filename='hello.html')

    可以直接传某个路径的文件名。

    快速体验

    现在我们以本地文件为例,传入一个名字为 hello.html 的文件,文件内容为

    <div>
    <ul>
    <li class="item-0">first item</li>
    <li class="item-1"><a href="link2.html">second item</a></li>
    <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
    <li class="item-1 active"><a href="link4.html">fourth item</a></li>
    <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul>
    </div>
     

    编写如下程序

    from pyquery import PyQuery as pq
    doc = pq(filename='hello.html')
    print doc.html()
    print type(doc)
    li = doc('li')
    print type(li)
    print li.text()

    运行结果

     
     <ul>
    <li class="item-0">first item</li>
    <li class="item-1"><a href="link2.html">second item</a></li>
    <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
    <li class="item-1 active"><a href="link4.html">fourth item</a></li>
    <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul> <class 'pyquery.pyquery.PyQuery'>
    <class 'pyquery.pyquery.PyQuery'>
    first item second item third item fourth item fifth item

    看,回忆一下 jQuery 的语法,是不是运行结果都是一样的呢?

    在这里我们注意到了一点,PyQuery 初始化之后,返回类型是 PyQuery,利用了选择器筛选一次之后,返回结果的类型依然还是 PyQuery,这简直和 jQuery 如出一辙,不能更赞!然而想一下 BeautifulSoup 和 XPath 返回的是什么?列表!一种不能再进行二次筛选(在这里指依然利用 BeautifulSoup 或者 XPath 语法)的对象!

    然而比比 PyQuery,哦我简直太爱它了!

    属性操作

    你可以完全按照 jQuery 的语法来进行 PyQuery 的操作。

    from pyquery import PyQuery 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")
     

    运行结果

    hello
    <p id="plop" class="hello"/>
    <p id="hello" class="hello"/>

    再来一发

    from pyquery import PyQuery as pq
    
    p = pq('<p id="hello" class="hello"></p>')('p')
    print p.addClass('beauty')
    print p.removeClass('hello')
    print p.css('font-size', '16px')
    print p.css({'background-color': 'yellow'})

    运行结果

    <p id="hello" class="hello beauty"/>
    <p id="hello" class="beauty"/>
    <p id="hello" class="beauty" style="font-size: 16px"/>
    <p id="hello" class="beauty" style="font-size: 16px; background-color: yellow"/>
  • 依旧是那么优雅与自信!

    在这里我们发现了,这是一连串的操作,而 p 是一直在原来的结果上变化的。

    因此执行上述操作之后,p 本身也发生了变化。

    DOM操作

    同样的原汁原味的 jQuery 语法

    from pyquery import PyQuery as pq
    
    p = pq('<p id="hello" class="hello"></p>')('p')
    print p.append(' check out <a href="http://reddit.com/r/python"><span>reddit</span></a>')
    print p.prepend('Oh yes!')
    d = pq('<div class="wrap"><div id="test"><a href="http://cuiqingcai.com">Germy</a></div></div>')
    p.prependTo(d('#test'))
    print p
    print d
    d.empty()
    print d

    运行结果

    <p id="hello" class="hello"> check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>
    <p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>
    <p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>
    <div class="wrap"><div id="test"><p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><a href="http://cuiqingcai.com">Germy</a></div></div>
    <div class="wrap"/>
     

    这不需要多解释了吧。

    DOM 操作也是与 jQuery 如出一辙。

    遍历

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

    from pyquery import PyQuery as pq
    doc = pq(filename='hello.html')
    lis = doc('li')
    for li in lis.items():
    print li.html() print lis.each(lambda e: e)

    运行结果

    first item
    <a href="link2.html">second item</a>
    <a href="link3.html"><span class="bold">third item</span></a>
    <a href="link4.html">fourth item</a>
    <a href="link5.html">fifth item</a>
    <li class="item-0">first item</li>
    <li class="item-1"><a href="link2.html">second item</a></li>
    <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
    <li class="item-1 active"><a href="link4.html">fourth item</a></li>
    <li class="item-0"><a href="link5.html">fifth item</a></li>

    不过最常用的还是 items 方法

    网页请求

    PyQuery 本身还有网页请求功能,而且会把请求下来的网页代码转为 PyQuery 对象。

    from pyquery import PyQuery as pq
    print pq('http://cuiqingcai.com/', headers={'user-agent': 'pyquery'})
    print pq('http://httpbin.org/post', {'foo': 'bar'}, method='post', verify=True)
     

    感受一下,GET,POST,样样通。

    Ajax

    PyQuery 同样支持 Ajax 操作,带有 get 和 post 方法,不过不常用,一般我们不会用 PyQuery 来做网络请求,仅仅是用来解析。

    PyQueryAjax

    API

    最后少不了的,API大放送。

    API

    原汁原味最全的API,都在里面了!如果你对 jQuery 语法不熟,强烈建议先学习下 jQuery,再回来看 PyQuery,你会感到异常亲切!

    结语

    用完了 PyQuery,我已经深深爱上了他!

    你呢?

python爬虫神器PyQuery的使用方法的更多相关文章

  1. Python网络爬虫神器PyQuery的使用方法

    #!/usr/bin/env python # -*- coding: utf-8 -*- import requests from pyquery import PyQuery as pq url ...

  2. Python爬虫之PyQuery使用(六)

    Python爬虫之PyQuery使用 PyQuery简介 pyquery能够通过选择器精确定位 DOM 树中的目标并进行操作.pyquery相当于jQuery的python实现,可以用于解析HTML网 ...

  3. Python 爬虫的代理 IP 设置方法汇总

    本文转载自:Python 爬虫的代理 IP 设置方法汇总 https://www.makcyun.top/web_scraping_withpython15.html 需要学习的地方:如何在爬虫中使用 ...

  4. python 爬虫数据存入csv格式方法

    python 爬虫数据存入csv格式方法 命令存储方式:scrapy crawl ju -o ju.csv 第一种方法:with open("F:/book_top250.csv" ...

  5. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)

    转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...

  6. Python爬虫beautifulsoup4常用的解析方法总结(新手必看)

    今天小编就为大家分享一篇关于Python爬虫beautifulsoup4常用的解析方法总结,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧摘要 如何用beau ...

  7. [python爬虫] Selenium常见元素定位方法和操作的学习介绍

    这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...

  8. python爬虫之PyQuery的基本使用

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  9. 【Python爬虫】PyQuery解析库

    PyQuery解析库 阅读目录 初始化 基本CSS选择器 查找元素 遍历 获取信息 DOM操作 伪类选择器 PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎 ...

随机推荐

  1. 重新认识unicode和utf8编码

    重新认识unicode和utf8编码 直到今天,准确的说是刚才,我才知道UTF-8编码和Unicode编码是不一样的,是有区别的囧 他们之间是有一定的联系的,看看他们的区别: UTF-8的长度是不一定 ...

  2. CentOS 6.x安装Chromium

    在CentOS/RHEL 7出来之前继续使用Chrome怎么办?使用Chrome的开源版本:Chromium. 1.切换到root: su - 或者 sudo -i 2.下载新的软件源定义: cd / ...

  3. Spring表单参数绑定中对“is”开头的boolean类型字段的的处理

    之前在新浪微博上面发了一个微薄: 弱弱的发现在定义boolean类型的时候最好不要使用“is”开头,可以避免一些问题哦 然后有一些朋友朋友问我为什么,当时比较忙,现在写篇文章举一个例子,回复一下这个问 ...

  4. PHPstorm激活

    最近想学习一下PHP 于是下载了很不错的phpstorm  但这老外的工具是要购买正版的 所以就搜了一下破解激活的教程 发现现在网上的在线破解在2016.2版本里面大多已被封杀 尝试了本地破解也发现大 ...

  5. ( 译、持续更新 ) JavaScript 上分小技巧(三)

    最近家里杂事较多,自学时间实在少的可怜,所以都在空闲时间看看老外写的内容,学习之外顺便翻译分享~等学习的时间充足些再写写自己的一些学习内容和知识点分析(最近有在接触的:复习(C#,SQL).(学习)T ...

  6. codevs 1001 舒适的路线(Kruskal)

    传送门 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤5 ...

  7. UVALive 3989Ladies' Choice(稳定婚姻问题)

    题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...

  8. DNS部署(centos 6)

    DNS部署(主从) 安装环境:CentOS 6.8 准备两台主机:192.168.137.13(主DNS).192.168.137.14(从DNS) EPEL仓库使用阿里源 rpm -ivh http ...

  9. git push to nas

    1 建nas目录 在nas的/volume1/git_repos目录下新建相关的目录,并在该目录下运行git init --bare cd /volume1/git_repos mkdir wifi_ ...

  10. CSS 图片加载完成再淡入显示

    一.方法 加载完成再显示:借助Image对象的onload事件,加载完时再把src赋给img标签的src: 淡人显示:起始opacity为0,利用transform过度到1 二.代码 <!DOC ...