【py分析】
pyQuery
pyQuery 是 jQuery 在 python 中的实现,能够以 jQuery 的语法来操作解析 HTML 文档,十分方便。使用前需要安装,easy_install pyquery 即可,或者 Ubuntu 下
sudo apt-get install python-pyquery |
以下例子:
from pyquery import PyQuery as pyq |
--------------- my code --------------------
for i in cts:
print '-'*10,pyq(i).find('h4').text()
for j in pyq(i).find('.subtitle'):
print pyq(j).text()
print '\n'
for j in pyq(i).find('.sublist'):
print '\t',pyq(j).text()
print '\n'
------------------------------------------------
You can use the PyQuery class to load an xml document from a string, a lxml document, from a file or from an url:
>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> import urllib
>>> d = pq("<html></html>")
>>> d = pq(etree.fromstring("<html></html>"))
>>> d = pq(url=your_url)
>>> d = pq(url=your_url,
... opener=lambda url, **kw: urlopen(url).read())
>>> d = pq(filename=path_to_html_file)
转换 (Traversing)
支持大部分jQuwey转换方法。这里是一些实例。
- 用字符选择器来进行过滤:
>>> d('p').filter('.hello')
[<p#hello.hello>]
- 也可以对单一元素使用 eq 方法:
>>> d('p').eq(0)
[<p#hello.hello>]
- 用户也可以寻找内嵌元素:
>>> d('p').find('a')
[<a>, <a>]
>>> d('p').eq(1).find('a')
[<a>]
>>> d('p').find('a').end()
[<p#hello.hello>, <p#test>]
>>> d('p').eq(0).end()
[<p#hello.hello>, <p#test>]
>>> d('p').filter(lambda i: i == 1).end()
[<p#hello.hello>, <p#test>]
【py分析】的更多相关文章
- 【py分析网页】可能有用的-re去除网页上的杂碎
def remove_js_css (content): """ remove the the javascript and the stylesheet and the ...
- 【py分析】使用SGMLParser分析淘宝html
SGMLParser Python 默认自带 HTMLParser 以及 SGMLParser 等等解析器,前者实在是太难用了,我就用 SGMLParser 写了一个示例程序: import urll ...
- twisted的tcp.py分析
#每个connector都有一个 Connection对象@implementer(interfaces.ITCPTransport, interfaces.ISystemHandle) class ...
- Django admin 组件 原理分析与扩展使用 之 sites.py (一)
一 . 前言 Django 提供了admin 组件 为项目提供基本的管理后台功能(对数据表的增删改查). 本篇文章通过 admin源码 简单分析admin 内部原理 ,扩展使用方式,为以后进行定制和自 ...
- tornado架构分析1 从helloworld分析tornado架构
最近公司需要我写一个高性能RESTful服务组件.我之前很少涉及这种高性能服务器架构,帮公司和平时没事玩都是写脚本级别的东西.虽然好多基础组件(sphinx.logging.configparse等) ...
- Android/Linux boot time分析优化
如果需要优化boot time,就需要一个量化的工具来分析每个阶段的时间消耗.这种类型的优化特别适合使用基于timeline的图表,有着明显的时间顺序.要求不但能给出整个流程消耗的时间,还要能对流程进 ...
- python 内存泄漏调试
Python应用程序内存泄漏的调试 Quake Lee quakelee@geekcn.org 新浪网技术(中国)有限公司 Sina Research & Development Python ...
- Python socket编程应用
最近因为考试各种复习顺便刷电视剧,感觉跟小伙伴玩的越来越不开心了,一定是最近太闲了,恩.于是想研究一下代理服务器,下载了一份代码,发现竟然还涉及到socket编程,所以把之前网络课的socket聊天室 ...
- Android/Linux boot time优化
基于analyze_boot.py分析Android/Linux的kernel boot时间 1.修改HiKey的BoardConfig.mk文件,使能initcall_debug,增加dmesg b ...
随机推荐
- Average Cost (AVCO) Method
http://accountingexplained.com/financial/inventories/avco-method Average Cost (AVCO) Method Aver ...
- SQLSERER 中select排序问题
SELECT * FROM 表名 ORDER BY PageNO DESC 这种排序会排出这种效果:1, 11,2,20 select *, RIG ...
- Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()
For Class.getResourceAsStream(String name), if the name parameter doesn't start with a "/" ...
- JQuery:JQuery操作CSS类
JQuery:CSS类jQuery - 获取并设置 CSS 类,通过 jQuery,可以很容易地对 CSS 元素进行操作.jQuery 操作 CSSjQuery 拥有若干进行 CSS 操作的方法.我们 ...
- iOS:WebKit内核框架的应用与解析
原文:http://www.cnblogs.com/fengmin/p/5737355.html 一.摘要: WebKit是iOS8之后引入的专门负责处理网页视图的框架,其比UIWebView更加强大 ...
- HTML:Input元素标签的详细介绍
总结Input的标签: Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍.1,type=text输入类型是t ...
- Shell 小技巧
Shell 小技巧 ${} 的使用 截断变量 去掉左边 使用 # (最短匹配)或 ## (最长匹配)方法为 ${var#<模式>} var=DUMMY echo ${var#*M} # M ...
- 开启mysql sql追踪
my.ini [mysqld] # The next three options are mutually exclusive to SERVER_PORT below. # skip-network ...
- Activity的四种启动模式和onNewIntent()
转自:http://blog.csdn.net/linghu_java/article/details/17266603 Android中Activity启动模式详解 在Android中每个界面都 ...
- 随机数产生random
随机数产生推荐用random(),在产生随机数前要添加种子srandom((unsigned int)time(NULL)). SYNOPSIS #include <stdlib.h> l ...