1. SGMLParser:

  这里定义了一个Parse类,继承SGMLParser里面的方法。使用一个变量is_h4做标记判定html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Parse的变量name中。解释一下start_h4()和end_h4()函数,他们原型是SGMLParser中的

start_tagname(self, attrs)
end_tagname(self)

tagname就是标签名称,比如当遇到<h4>,就会调用start_h4,遇到</h4>,就会调用 end_h4。attrs为标签的参数,以[(attribute, value), (attribute, value), ...]的形式传回。

Demo:

 #!/usr/bin/python2.7
# FileName: sgmlparser.py
# Author: lxw
# Date: 2015-07-30 import urllib2
from sgmllib import SGMLParser class Parse(SGMLParser):
def __init__(self):
SGMLParser.__init__(self)
self.is_h4 = ""
self.name = []
self.is_a = ""
self.link = [] def start_h4(self, attrs):
self.is_h4 = 1 def end_h4(self):
self.is_h4 = "" def start_a(self, attrs):
self.is_a = 1 def end_a(self):
self.is_a = "" def handle_data(self, text):
if self.is_h4 == 1:
self.name.append(text)
if self.is_a == 1:
self.link.append(text) def main():
#content = urllib2.urlopen("https://kb.isc.org/").read()
content = urllib2.urlopen("https://list.taobao.com/browse/cat-0.htm").read()
parse = Parse()
parse.feed(content)
for item in parse.link:
print(item.decode("gbk").encode("utf-8"))
print("-"*20)
for item in parse.name:
print(item.decode("gbk").encode("utf-8")) if __name__ == '__main__':
main()
else:
print("Being imported as a module.")

2. PyQuery:

#!/usr/bin/python2.7
#coding=utf-8
#如果想有中文注释就必须得有上面的语句
# FileName: pyQueryParse.py
# Author: lxw
# Date: 2015-07-30 from pyquery import PyQuery
'''
直接运行没有问题, 但当把输出重定向到文件时, 就出现如下错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 166-167: ordinal not in range(128)
解决方法是增加下面的三行代码:
'''
import sys
reload(sys)
sys.setdefaultencoding("utf-8") def main():
source = PyQuery(url="https://list.taobao.com/browse/cat-0.htm")
#print(type(source)) #<class 'pyquery.pyquery.PyQuery'>
#print(type((source("a")))) #<class 'pyquery.pyquery.PyQuery'>
for data in source.find("a"):
#print(type(data)) #<class 'lxml.html.HtmlElement'>
#print(type(PyQuery((data)))) #<class 'pyquery.pyquery.PyQuery'>
#print(type(PyQuery(data).text())) #<type 'unicode'>/<type 'str'>
print(PyQuery(data).text()) if __name__ == '__main__':
main()
else:
print("Being imported as a module.")

References:

Python写爬虫——抓取网页并解析HTML

python数据抓取之pyquery包

Python HTML Resolution Demo - SGMLParser & PyQuery的更多相关文章

  1. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

  2. Python爬虫利器六之PyQuery的用法

    前言 你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有 ...

  3. pyhanlp python 脚本的demo补充

    java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...

  4. python爬虫(10)--PyQuery的用法

    简介 pyquery 可让你用 jQuery 的语法来对 xml 进行操作.这I和 jQuery 十分类似.如果利用 lxml,pyquery 对 xml 和 html 的处理将更快. 初始化 在这里 ...

  5. Python的网页解析库-PyQuery

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

  6. Python简单多进程demo

    ''' 多线程使用场景: 怎样用Python的多线程提高效率? io操作不占用CPU 计算操作占用CPU Python多线程不适合CPU操作密集型的任务,适合io操作密集型的任务 如果有CPU操作密集 ...

  7. python - hadoop,mapreduce demo

    Hadoop,mapreduce 介绍 59888745@qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统(cloudera是最大的输出者类似于Linux的红帽), 把用户的交易 ...

  8. python ros topic demo

    发布者: #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker():   ...

  9. 转 python trace walk DEMO

    https://blog.csdn.net/steadfast123/article/details/46965125 #quote from 'introduction to computation ...

随机推荐

  1. matlab和C/C++混合编程--Mex (转载)

    matlab和C/C++混合编程--Mex 最近的项目需要matlab和C的混合编程,经过一番努力终于完成了项目要解决的问题.现在就将Mex的一些经验总结一下,当然只是刚刚开始,以后随着学习的深入继续 ...

  2. 如何编写Makefile?

    //swap.c #include<stdio.h> int swap(int *x,int *y) {printf("a=%d b=%d\n",*x,*y); int ...

  3. openssl基础

    OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. OpenSSL is an open source ...

  4. php服务器端与android客户端通信问题

    http://www.oschina.net/question/616446_90760

  5. 1年3年5年-我对PHP攻城师有看法

    今天早上公车上看微信拉勾的一些岗位推送,挑了几个PHP攻城师看看 15K-20K的 百万级网站架构经验 3年以上开发,至少1年互联网用户产品开团队开发经验 不低于百度T4水平 数据库规划和优化,熟悉常 ...

  6. git 工作模式

    个人在学习Git工作流的过程中,从原有的 SVN 模式很难完全理解Git的协作模式,直到有一天我看到了下面的文章,好多遗留在心中的困惑迎刃而解: 我们以使用SVN的工作流来使用Git有什么不妥? Gi ...

  7. NIPS(Conference and Workshop on Neural Information Processing Systems)

    论文提交时间:5月下旬 会议时间:12月上旬 NIPS2017: 网址:https://nips.cc/

  8. 竞赛图的得分序列 (SRM 717 div 1 250)

    SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, ...

  9. Linux文件的打包与压缩

    打包命令: tar tar 的选项与参数非常的多!我们只讲几个常用的选项,更多选项您可以自行 man tar 查询罗! [root@www ~]# tar [-j|-z] [cv] [-f 创建的档名 ...

  10. Linux USB 鼠标输入驱动具体解释

    平台:mini2440 内核:linux 2.6.32.2 USB设备插入时.内核会读取设备信息,接着就把id_table里的信息与读取到的信息做比較.看是否匹配,假设匹配.就调用probe函数. U ...