Python 通过sgmllib模块解析HTML
"""
对html文本的解析方案-示例:在标签开始的时候检查标签中的attrs属性,解析出所有的参数的href属性值
依赖安装:pip install sgmllib3k
使用方法:
1.自定义一个类,继承sgmllib的SGMLParser
2.复写SGMLParser的方法,添加自己自定义的标签处理函数
3.通过自定义的类的对象的.feed(data)把要解析的数据传入解析器,然后自定义的方法自动生效。
"""
from urllib import request
import sgmllib class HandleHtml(sgmllib.SGMLParser):
"""
自定义HTML解析类
""" def unknown_starttag(self, tag, attrs):
"""
任意标签开始被解析时调用
:param tag: 标签名
:param attrs: 标签的参数
:return:
"""
try:
for attr in attrs:
if attr[0] == 'href':
print(f"{attr[0]}:{attr[1]}")
except:
pass if __name__ == '__main__':
response = request.urlopen("http://freebuf.com/")
page = response.read()
page = page.decode('utf-8') # 创建HTML解析对象
handle_html = HandleHtml()
# 将数据传入解析器
handle_html.feed(page)
输出结果:
href:https://www.freebuf.com/buf/plugins/wp-favorite-posts/wpfp.css
href:https://static.3001.net/css/recentcomments/wp-recentcomments.css?ver=2.2.3
href:https://www.freebuf.com/buf/plugins/gold/assets/css/widget.css?ver=1.3.2.1
href:https://static.3001.net/css/highslide/highslide.css
href:https://www.freebuf.com/buf/plugins/cartpauj-pm/style/style.css
href: https://www.freebuf.com/buf/plugins/simditor/highlight/styles/default.css
href:https://static.freebuf.com/images/favicon.ico
href:https://static.3001.net/css/new/header.css
href:https://static.3001.net/css/new/bootstrap.min.css?ver=2016051701
href:https://static.3001.net/css/new/swiper-3.4.2.min.css
href:https://static.3001.net/css/new/model.css?ver=2017112156855
href:https://static.3001.net/css/new/style.css?ver=2018112123749359438534
href:http://www.freebuf.com
href:http://www.freebuf.com
href:http://job.freebuf.com
href:#
......
Python 通过sgmllib模块解析HTML的更多相关文章
- Python中pandas模块解析
Pandas基于两种数据类型: series 与 dataframe . 1.Series 一个series是一个一维的数据类型,其中每一个元素都有一个标签.类似于Numpy中元素带标签的数组.其中, ...
- Python中matplotlib模块解析
用Matplotlib绘制二维图像的最简单方法是: 1. 导入模块 导入matplotlib的子模块 import matplotlib.pyplot as plt import numpy as ...
- Python中csv模块解析
导入模块 import csv 2.读取csv文件 file1 = open('test1.csv', 'rb') reader = csv.reader(file1) rows = [row for ...
- Python中xlrd模块解析
xlrd 导入模块 import xlrd 2.打开指定的excel文件,返回一个data对象 data = xlrd.open_workbook(file) ...
- python xml.dom模块解析xml
1. 什么是xml?有何特征? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 例子:del.xml <?xml version=&q ...
- Python之select模块解析
首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...
- python之poplib模块下载并解析邮件
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之poplib模块下载并解析邮件 #https://github.com/michaelliao ...
- Python命令行参数解析模块getopt使用实例
Python命令行参数解析模块getopt使用实例 这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参 ...
- python获取命令行传参的两种种常用方法argparse解析getopt 模块解析
方法一:argparse解析 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Time: 2020/5/20 10:38 # @Author:zhan ...
随机推荐
- scp出现ssh port 22: Connection refused 问题解决具体步骤
[root(0)@sys11 09:20:29 /home/work/Code_release/bj]# scp ./release.sh root@192.168.161.151:/Users/a ...
- HTML5对表单的一些有意思的改进
HTML5对表单进行了许多的改进,在这篇文章中,我选择了其中个人认为很有趣的三个变化:自动聚焦,对button元素的改进,把表单元素与非父元素的form表单挂钩进行简单的介绍. 1. 自动聚焦 自动聚 ...
- 【题解】洛谷P2704 [NOI2001] 炮兵阵地(状压DP)
洛谷P2704:https://www.luogu.org/problemnew/show/P2704 思路 这道题一开始以为是什么基于状压的高端算法 没想到只是一道加了一行状态判断的状压DP而已 与 ...
- 应对STM32 Cortex-M3 Hard Fault异常
STM32 Cortex-M3 Hard Fault Hard fault (硬错误,也有译为硬件错误的)是在STM32(如无特别说明,这里的STM32指的是Cortex-M3的核)上编写程序中所产生 ...
- render 函数渲染表格的当前数据列使用
columns7: [ { title: '编号', align: 'center', width: 90, key: 'No', render: (h, params) => { return ...
- OC中property方法的使用
我们直入主题,关于property方法,我们先来了解一下相关的知识,首先是成员变量,实例变量,属性变量. 我们定义一个类来看一下 @interface Person :NSObject{ NSInte ...
- 737 MAX 8-Think
波音 737 MAX 8的事故,凸显传感器数据在失真的情况下,软件系统需要如何设计的问题:这点感觉波音那么大的公司,不应该不会没有考虑到. 正常来说传感器给出错误的数据,通常是计算出错误的结果,就像做 ...
- Docker镜像浅谈
先抛出几个我在学习过程中产生的几个问题. 1. 容器镜像是什么, 和装系统时的镜像有什么关系? 2. 容器镜像的作用是什么? 3. 不同版本的ubuntu镜像有什么区别, 比如说 ubuntu:18. ...
- LAMP+Varnish的实现
基于Keepalived+Varnish+Nginx实现的高可用LAMP架构 注意:各节点的时间需要同步(ntpdate ntp1.aliyun.com),关闭firewalld(systemctl ...
- python三大神器之装饰器
装饰器的形成过程 假如你要写一个计算函数执行时间的函数,代码如下: import time def func1(): print('in func1') def timer(func): def in ...