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 ...
随机推荐
- (译)Getting Started——1.1.1 Start Developing IOS Today(开始IOS开发)
安装 本课程对于创建运行在iPad.iPhone和iPod触摸屏上的应用来说,是一个完美的起点.该向导的四个板块可以作为构建你第一个应用的简单向导——内容包括了你需要使用的工具,主要的理念 ...
- SQL语句教程
SQL指令 SELECT DISTINCT WHERE AND OR IN BETWEEN LIKE ORDER BY 函数 COUNT GROUP BY HAVING ALIAS 表格链接 外部链接 ...
- 第二百一十七节,jQuery EasyUI,NumberSpinner(数字微调)组件
jQuery EasyUI,NumberSpinner(数字微调)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 NumberSpinner ...
- VS2010之– Web Development(四)-将WebApplication打包发布到IIS
下面将介绍怎样将一个WebApplication在VisualStudio中直接打包发布到IIS上去. 首先需要使用Administrator的身份运行Visual Studio. Task 1 – ...
- ICO图标的制作与应用
制作参看:http://www.shouce.ren/tool/ico?action=make 示例: <link href="./js/favicon.ico" rel=& ...
- 【ARDUINO】串口无法打开问题
1.查看是否串口被锁 sudo arduino ls /var/lock sudo rm /var/lock/LCK..ttyACM* 2.查看arduino安装位置 dpkg -S XXXX 3.原 ...
- Unity3D入门其实很简单
在上次发布拙作后,有不少童鞋询问本人如何学习Unity3D.本人自知作为一名刚入门的菜鸟,实在没有资格谈论这么高大上的话题,生怕误导了各位.不过思来想去,决定还是写一些自己的经验,如果能给想要入门U3 ...
- webpack文档翻译
https://segmentfault.com/a/1190000007568507
- Android实例-退出程序
Android实例-退出程序 http://www.cnblogs.com/FKdelphi unit Unit1; interface uses System.SysUtils, System.Ty ...
- JZOJ.5273【NOIP2017模拟8.14】亲戚
Description