今天小编给大家分享的是怎么在html页面中调用外部样式,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。两种调用方法:1、使用link标签调用,语
顺晟科技
2021-08-28 11:16:51
158
类的定义
常用方法
try: from HTMLParser import HTMLParser except: from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.data = [] # 定义data数组用来存储html中的数据 self.links = [] def handle_starttag(self, tag, attrs): print(\'<%s>\' % tag) if tag == "a": if len(attrs) == 0: pass else: for (variable, value) in attrs: if variable == "href": self.links.append(value) def handle_endtag(self, tag): print(\'</%s>\' % tag) def handle_startendtag(self, tag, attrs): print(\'<%s/>\' % tag) def handle_data(self, data): print(\'data===>\', data) def handle_comment(self, data): print(\'<!--\', data, \'-->\') def handle_entityref(self, name): print(\'&%s;\' % name) def handle_charref(self, name): print(\'&#%s;\' % name) if __name__ == "__main__": html_code = \'\'\'<html> <head>这是头标签</head> <body> <!-- test html parser --> <p>Some <a href="#">html</a> HTML Ӓ Ӓtutorial...<br>END</p> </body></html>\'\'\' parser = MyHTMLParser() parser.feed(html_code) parser.close() print(parser.data) print(parser.links)
在 HTML 中 <、>、& 等字符有特殊含义(<,> 用于标签中,& 用于转义),他们不能在 HTML 代码中直接使用,如果要在网页中显示这些符号,就需要使用 HTML 的转义字符串(Escape Sequence),例如 < 的转义字符是 <,浏览器渲染 HTML 页面时,会自动把转移字符串换成真实字符。
转义字符(Escape Sequence)由三部分组成:部分是一个 & 符号,第二部分是实体(Entity)名字,第三部分是一个分号。 比如,要显示小于号(<),就可以写 <。
html = \'<abc>\' # 反转义:方式1 try: from HTMLParser import HTMLParser except: from html.parser import HTMLParser html_parser = HTMLParser() text = html_parser.unescape(html) print(text) # 反转义:方式2 import html text = html.unescape(\'a=1&b=2\') print(text) # 转义 import cgi html = cgi.escape(text) print(html)
https://www.liaoxuefeng.com/wiki/897692888725344/966401234683424
https://www.liaoxuefeng.com/wiki/1016959663602400/1017784593019776
https://baijiahao.baidu.com/s?id=1637614366297669334&wfr=spider&for=pc
19
2022-10
17
2022-10
02
2022-10
01
2022-10
30
2022-09
25
2022-09