"""
对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的更多相关文章

  1. Python中pandas模块解析

    Pandas基于两种数据类型: series 与 dataframe . 1.Series 一个series是一个一维的数据类型,其中每一个元素都有一个标签.类似于Numpy中元素带标签的数组.其中, ...

  2. Python中matplotlib模块解析

    用Matplotlib绘制二维图像的最简单方法是: 1.  导入模块 导入matplotlib的子模块 import matplotlib.pyplot as plt import numpy as ...

  3. Python中csv模块解析

    导入模块 import csv 2.读取csv文件 file1 = open('test1.csv', 'rb') reader = csv.reader(file1) rows = [row for ...

  4. Python中xlrd模块解析

    xlrd 导入模块 import xlrd 2.打开指定的excel文件,返回一个data对象 data = xlrd.open_workbook(file)                     ...

  5. python xml.dom模块解析xml

    1. 什么是xml?有何特征? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 例子:del.xml <?xml version=&q ...

  6. Python之select模块解析

    首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...

  7. python之poplib模块下载并解析邮件

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之poplib模块下载并解析邮件 #https://github.com/michaelliao ...

  8. Python命令行参数解析模块getopt使用实例

    Python命令行参数解析模块getopt使用实例 这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参 ...

  9. python获取命令行传参的两种种常用方法argparse解析getopt 模块解析

    方法一:argparse解析 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Time: 2020/5/20 10:38 # @Author:zhan ...

随机推荐

  1. Linux磁盘分区,挂载

    分区基础知识 分区的方式:   1) mbr分区:     1.最多支持四个主分区     2.系统只能安装在主分区     3.扩展分区要占一个主分区     4.MBR最大只支持2TB,但拥有最好 ...

  2. Dos小技巧-在Dos中直接打开软件

    在這裡我們舉一個例子 在D盤的D:\Program Files (x86)\Youdao\Dict4下面是關於有道字典的基本信息如圖(1.1.1).當點擊uninst.exe的時候顯示如圖:(1.1. ...

  3. SpringMVC使用校验validator校验对象属性

    1.pom.xm添加依赖 <dependency> <groupId>javax.validation</groupId> <artifactId>va ...

  4. PAT——1017. A除以B

    本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...

  5. 用js写三个数,让三个数从小到大排列

    console.log('请输入三个数:'); let num1 = readline.question() - 0; let num2 = readline.question() - 0; let ...

  6. 课时18.h标签和p标签以及hr标签(掌握)

    如何在webstorm中利用快捷键创建一个新的html的文件? 同时按下键盘上的ctrl+alt+insert(windows) 同时按下键盘上的ctrl+alt+n(os) h标签系列(header ...

  7. 在vue-cli + webpack 项目中使用sass

    1.准备工作: 由于npm的服务器在国外,网速慢而且安装容易失败,建议在安装之前,先安装国内的镜像,比如淘宝镜像 npm install -g cnpm --registry=https://regi ...

  8. c和c++单链表

    c++版 #include<iostream> #include<malloc.h> using namespace std; struct node{ int data; n ...

  9. html中的定位

    html中的定位体系 一. 分类 1.常规流static 2.浮动float 3.相对定位relative 4.绝对定位absolute 5.固定定位fixed 二.使用时的区分 在网页布局中,常常都 ...

  10. Spring Security学习笔记(一)

    认证和权限控制 AuthenticationManager是认证的主要接口,它只有一个authenticate方法,可以做3件事情. 返回一个认证信息(Authentication),表示认证成功 抛 ...