python去除html标签
㈠ 用python正则替换HTML中pre标签里面的特殊符号
一共就7个符号,就写7行替换吧。
用不用正则无所谓,不多。
不用正则也行,网页parse后,innerText输出的就是正常文本,innerHtml输出的才是你说的这种有特殊符号的内容。
㈡ python 解析html 什么包好
对html的解析是网页抓取的基础,分析抓取的结果找到自己想要的内容或标签以达到抓取的目的。
HTMLParser是python用来解析html的模块。它可以分析出html里面的标签、数据等等,是一种处理html的简便途径。 HTMLParser采用的是一种事件驱动的模式,当HTMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理。它主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然后重新定义这几个以handler_开头的函数即可。这几个函数包括:
handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx> tag不区分大小写
handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
handle_pi 处理形如<?instruction>的东西
def handle_starttag(self,tag,attr):
#注意:tag不区分大小写,此时也可以解析 <A 标签
# SGMLParser 会在创建attrs 时将属性名转化为小写。
if tag=='a':
for href,link in attr:
if href.lower()=="href":
pass
1. 基本解析,找到开始和结束标签
[python] view plain
<span style="font-size:18px;">#coding:utf-8
from HTMLParser import HTMLParser
'''''
HTMLParser的成员函数:
handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx>
handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
handle_pi 处理形如<?instruction>的东西
'''
class myHtmlParser(HTMLParser):
#处理<!开头的内容
def handle_decl(self,decl):
print 'Encounter some declaration:'+ decl
def handle_starttag(self,tag,attrs):
print 'Encounter the beginning of a %s tag' % tag
def handle_endtag(self,tag):
print 'Encounter the end of a %s tag' % tag
#处理注释
def handle_comment(self,comment):
print 'Encounter some comments:' + comment
if __name__=='__main__':
a = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
<html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">链接到163</a></body></html>'
m=myHtmlParser()
m.feed(a)
m.close()
输出结果:
Encounter some declaration:DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
Encounter the beginning of a html tag
Encounter the beginning of a head tag
Encounter some comments:insert javaScript here!
Encounter the beginning of a title tag
Encounter the end of a title tag
Encounter the beginning of a body tag
Encounter the beginning of a a tag
Encounter the end of a a tag
Encounter the end of a body tag
Encounter the end of a html tag</span>
㈢ Python编写一个程序以尝试解析XML/HTML标签.
要给这段文字添加一个 root 标签,然后对里面的 node 进行遍历。root 标签的名字可以任意定(但是必须添加一个),我这里使用的 root 命名,对于其它的名字也一样。如果你是直接读取的 XML 文件,而不是字符串,可以将文件打开,然后把文件句柄传入 ElementTree.parse() 函数,最后对其返回值进行遍历。
fromxml.etreeimportElementTree
parsed=ElementTree.XML('''<root>
<composer>WolfgangAmadeusMozart</composer><author>SamuelBeckett</author><city>London</city>
</root>''')
outstr=[]
fornodeinparsed:
outstr+=['%s:%s'%(node.tag,node.text)]
print(''.join(outstr))
㈣ python的json.loads如何处理带HTML的字符串
json串中的双引号需要转义为【\"】:
str='''[{"level": 1,"value": ["<p>aaa\"b\"ccc</p>"]}]'''
㈤ python 剔除文本中不需要的标签和连接,提取中文和图片的连接
import re 利用正则提取,简单方便。
importre
text=''#待提取文本
result1=re.findall(r'([u4e00-u9fa5]+)',text)#提取中文
result2=re.findall(r'<img.*?src="(.*?)"[^>]*?>',text,re.S)#提取图片链接
print(result1)
print(result2)
㈥ python语言去除文本中的p标签
用Python语言的sub()函数替换就可以实现你的目标
具体程序如下(假设你每次从文件中读取一行,放在line变量中)
importre
line='<p>宁波大学</p>'
regex=r'</?p>'
result=re.sub(regex,"",line,re.I)
print(result)