Python HTML Resolution Demo - SGMLParser & PyQuery
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 Resolution Demo - SGMLParser & PyQuery的更多相关文章
- RPi 2B python opencv camera demo example
/************************************************************************************** * RPi 2B pyt ...
- Python爬虫利器六之PyQuery的用法
前言 你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有 ...
- pyhanlp python 脚本的demo补充
java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...
- python爬虫(10)--PyQuery的用法
简介 pyquery 可让你用 jQuery 的语法来对 xml 进行操作.这I和 jQuery 十分类似.如果利用 lxml,pyquery 对 xml 和 html 的处理将更快. 初始化 在这里 ...
- Python的网页解析库-PyQuery
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...
- Python简单多进程demo
''' 多线程使用场景: 怎样用Python的多线程提高效率? io操作不占用CPU 计算操作占用CPU Python多线程不适合CPU操作密集型的任务,适合io操作密集型的任务 如果有CPU操作密集 ...
- python - hadoop,mapreduce demo
Hadoop,mapreduce 介绍 59888745@qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统(cloudera是最大的输出者类似于Linux的红帽), 把用户的交易 ...
- python ros topic demo
发布者: #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker(): ...
- 转 python trace walk DEMO
https://blog.csdn.net/steadfast123/article/details/46965125 #quote from 'introduction to computation ...
随机推荐
- TP 框架 ajax[利用异步提交表单]
//[] $(function () { $("#send-btn" ).click(function (){ //接受表单的值 var username=$('input[nam ...
- 破解idea注册码
添加 “0.0.0.0 account.jetbrains.com”到host, hosts位置:C:\Windows\System32\drivers\etc 获取注册码网址: http://ide ...
- 哈希表类Hashtable
哈希表是一种重要的存储方式,也是一种常见的检索方法.其基本思想是将关系码的值作为自变量,通过一定的函数关系计算出对应的函数值,把这个数值解释为结点的存储地址,将结点存入计算得到存储地址所对应的存储单元 ...
- android Dialog 底部弹出
. if (dialShareDialog == null) { dialShareDialog = new Dialog(context, R.style.dialog); dialShareDia ...
- Attribute在.net编程中的应用(一)
Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一个非常重要的组成部分,为了帮助大家理解和掌握A ...
- Material design之New Widgets(RecyclerView CardView)
New Widgets:提供了两个新的控件 RecyclerView CardView 这两个控件包含在了Android L的support library中, 他们可以用于显示复杂的布局而且都默认采 ...
- ZBarReaderView屏幕旋转问题
转载:http://42.96.197.72/ios-zbarreaderview-interface-orientation/ 在iPad应用中,如果没有特殊情况,需要让应用支持所有屏幕方向.在iP ...
- leveldb学习笔记
LevelDB由 Jeff Dean和Sanjay Ghemawat开发. LevelDb是能够处理十亿级别规模Key-Value型数据持久性存储的C++ 程序库. 特别如下: 1.LevelDb是一 ...
- sql server 作业没跑、开启sql 代理服务、新建作业
sql server 数据库中设置了晚上跑的作业,以前没注意,后来换了服务器建了新的虚拟机后第二天发现作业没跑. 主动执行作业可以实现目的,但是他不会自动执行,那么问题来了,为啥呢? 没有开启SQL ...
- 【BZOJ2039】[2009国家集训队]employ人员雇佣 最小割
[BZOJ2039][2009国家集训队]employ人员雇佣 Description 作为一个富有经营头脑的富翁,小L决定从本国最优秀的经理中雇佣一些来经营自己的公司.这些经理相互之间合作有一个贡献 ...