python - PyQuery
偶尔的机会,知道这么个扩展,手贱翻了下文档,发现似乎挺有意思,遂记录一二。
what:
这是一个python版本的jquery,而且是后端执行的,至少官方是这么说的:
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.
what for:
可以用来:
1) 设定主题
2) html分析
why:
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.
How:
首先,作者假定网页是utf-8编码的。(如果不是请自行处理)
网页不是utf-8的,请添加以下处理逻辑:
import urllib2
from pyquery import PyQuery as pq url_instance = urllib2.urlopen('http://your_site')
page_text_raw = url_instance.read()
page_text_unicode = unicode(page_text_raw,'utf-8')
page_dom = pq(page_text_unicode)
然后,就可以向jquery一样调用(不是100%,作者都说as much as possible the similar to the JQuery ...)。
api如下:
主要请参考这里。
PyQuery.val(value=<NoDefault>)
设置DOM元素的value属性
PyQuery.wrap(value)
这个比较有用,可以同一为一组元素设定父容器,从而改变主题(Theme),demo如下:
d = pq('<div><span>Hey</span><span>you !</span></div>')
print d('span').wrap('<div></div>')
输出:
<div><span>Hey</span></div><div><span>you !</span></div>
备注,还有一个warpAll, 作用是将所有元素统一包在一起,
print d('span').wrapAll('<div></div>')
结果:
<div><span>Hey</span><span>you !</span></div>
PyQuery.toggleClass(value)
这个跟jquery的toggle类似。
html/text/val, 读取/设置html,text,value
其他略
python - PyQuery的更多相关文章
- python pyquery 基本用法
1.安装方法 pip install pyquery 2.引用方法 from pyquery import PyQuery as pq 3.简介 pyquery 是类型jquery 的一个专供pyth ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Python开源框架、库、软件和资源大集合
A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome- ...
- Python 库汇总英文版
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- python 抓取网上OJ试题
学校工作需要,需架设一台内网OJ服务器,采用了开源的hustoj.试题下载了hustoj的freeprblem的xml文件.导入时出现很多错误,不知什么原因.另外要将历年noip复赛试题加上去,但苦于 ...
- Python爬虫学习==>第五章:爬虫常用库的安装
学习目的: 爬虫有请求库(request.selenium).解析库.存储库(MongoDB.Redis).工具库,此节学习安装常用库的安装 正式步骤 Step1:urllib和re库 这两个库在安装 ...
- python爬虫神器PyQuery的使用方法
你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有了一些 ...
- windows下python安装pyquery
安装pyquery之前首先要明确一点,easyinstall 是一款python包管理器,类似于node的npm,用于安装python的扩展包,它安装的包是以*.egg的方式. 要安装pq需要经历以下 ...
- Python开发包推荐系列之xml、html解析器PyQuery
使用python,喜欢她的简洁是一方面,另外就是它有着丰富的开发包 好用又方便 接下来会给大家推荐一系列很赞的开发包. 在解析html.xml过程中,我们有不少的包可以用.比如bs.lxml.xmlt ...
随机推荐
- 2015级C程序设计课程开始了
今年重新接手软件工程文科班的教学,应更从的结合文科班学习基础与 学习方式对授课方式进行调整,以便能取得好的教学效果.
- easyui中tree控件添加自定义图标icon
来源于:http://blog.163.com/lintianhuanhai@126/blog/static/165587366201421704420256/ <!DOCTYPE html&g ...
- 使用Owin中间件搭建OAuth2.0认证授权服务器
前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...
- Eclipse属性文件编辑器---Properties Editor
今天在用 Eclipse 来编辑 .properties 文件时,写的中文会自动转为 Unicode 编码,完全不知道自己的中文写的是什么!! 于是查了一下,网上推荐,在Eclipse 中 安装一个 ...
- 基于Bootstrap的jQuery开关按钮插件
按钮 下载 使用方法 首先要在页面中引入依赖文件: jquery.Bootstrap.Bootstrap Switch CSS和Bootstrap Switch JS.这里用的是bootstr ...
- 【CodeForces 614A】Link/Cut Tree
题 题意 给你一个区间,求里面有多少个数是k的次方. 分析 暴力,但是要注意这题范围会爆long long,当k=1e8: l=1:r=1e18时 k²=1e16,判断了是≤r,然后输出,再乘k就是1 ...
- collections_python
代码 import collections#counter继承字典的方法,items(),keys(),vavle() obj = collections.Counter('acbdafcbad') ...
- 最小圆覆盖(Smallest Enclosing Discs)
随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...
- android 事件处理概念簇
1)事件传递函数 2)事件相关: 事件.事件源.事件监听器.MotionEvent 3)坐标系.定位.路由: 4)Window activity view viewGroup
- CodeForces 705B (训练水题)
题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...