pyQuery

pyQuery 是 jQuery 在 python 中的实现,能够以 jQuery 的语法来操作解析 HTML 文档,十分方便。使用前需要安装,easy_install pyquery 即可,或者 Ubuntu 下

sudo apt-get install python-pyquery

以下例子:

from pyquery import PyQuery as pyq
doc=pyq(url=r'http://list.taobao.com/browse/cat-0.htm')
cts=doc('.market-cat')
 
for i in cts:
print '====',pyq(i).find('h4').text() ,'===='
for j in pyq(i).find('.sub'):
print pyq(j).text() ,
print '\n'

--------------- 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分析】的更多相关文章

  1. 【py分析网页】可能有用的-re去除网页上的杂碎

    def remove_js_css (content): """ remove the the javascript and the stylesheet and the ...

  2. 【py分析】使用SGMLParser分析淘宝html

    SGMLParser Python 默认自带 HTMLParser 以及 SGMLParser 等等解析器,前者实在是太难用了,我就用 SGMLParser 写了一个示例程序: import urll ...

  3. twisted的tcp.py分析

    #每个connector都有一个 Connection对象@implementer(interfaces.ITCPTransport, interfaces.ISystemHandle) class ...

  4. Django admin 组件 原理分析与扩展使用 之 sites.py (一)

    一 . 前言 Django 提供了admin 组件 为项目提供基本的管理后台功能(对数据表的增删改查). 本篇文章通过 admin源码 简单分析admin 内部原理 ,扩展使用方式,为以后进行定制和自 ...

  5. tornado架构分析1 从helloworld分析tornado架构

    最近公司需要我写一个高性能RESTful服务组件.我之前很少涉及这种高性能服务器架构,帮公司和平时没事玩都是写脚本级别的东西.虽然好多基础组件(sphinx.logging.configparse等) ...

  6. Android/Linux boot time分析优化

    如果需要优化boot time,就需要一个量化的工具来分析每个阶段的时间消耗.这种类型的优化特别适合使用基于timeline的图表,有着明显的时间顺序.要求不但能给出整个流程消耗的时间,还要能对流程进 ...

  7. python 内存泄漏调试

    Python应用程序内存泄漏的调试 Quake Lee quakelee@geekcn.org 新浪网技术(中国)有限公司 Sina Research & Development Python ...

  8. Python socket编程应用

    最近因为考试各种复习顺便刷电视剧,感觉跟小伙伴玩的越来越不开心了,一定是最近太闲了,恩.于是想研究一下代理服务器,下载了一份代码,发现竟然还涉及到socket编程,所以把之前网络课的socket聊天室 ...

  9. Android/Linux boot time优化

    基于analyze_boot.py分析Android/Linux的kernel boot时间 1.修改HiKey的BoardConfig.mk文件,使能initcall_debug,增加dmesg b ...

随机推荐

  1. spring.net使用

    1.方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  2. [LeetCode]题解(python):054-Spiral Matrix

    题目来源 https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n column ...

  3. Android笔记:java 中的数组

    在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...

  4. jQuery层次选择器

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. js中的类GET方法

    其实是通过js语句自己建的函数 function request(strParame){ var args = new Object(); ); var pairs = query.split(&qu ...

  6. python基础篇-day1

    python基础篇 python是由C语言写的: pass 占位符: del,python中全局的功能,删除内存中的数据: 变量赋值的方法: user,pass = 'freddy','freddy1 ...

  7. 使用iptables防火墙限制web的访问PV

    使用iptables防火墙限制web的访问PV #!/bin/bash while true;do awk '{print $1}' /tmp/test/access.log| grep -v &qu ...

  8. Xib的使用与File'Owner总结

    Xib的使用与File'Owner总结 一.XIB的适用范围 xib(也叫Nib)与storyboard一样是用来描述界面的. storyboard描述的是比较大型的,大范围.适合描述界面跳转等. 二 ...

  9. Visual Studio 2013发布网站

  10. UIActivityIndicatorView

    1.    activityIndicatorViewStyle 设置指示器的样式 UIActivityIndicatorViewStyleWhiteLarge UIActivityIndicator ...