導航:首頁 > 凈水問答 > python過濾html字元

python過濾html字元

發布時間:2021-11-29 09:16:31

『壹』 如何用python過濾html標簽和准確的提取內容

可以參考這個實例,代碼中有過濾html標簽及提取內容:

Python網頁爬蟲入門——抓取網路貼吧內容實例
http://lovesoo.org/getting-started-python-web-crawler-to-crawl-the--post-bar-content-instance.html

『貳』 python去掉html標簽

s='<SPANstyle="FONT-SIZE:9pt">開始1~3<SPANlang=EN-US><?xml:namespaceprefix=ons="urn:schemas-microsoft-com:office:office"/><o:p></o:p></SPAN></SPAN>'
importre
d=re.sub('<[^>]+>','',s)
printd
開始1~3

『叄』 python正則表達式過濾網頁div只保留其中的一個字元串

importre
s='<divclass="displayHeadingName"data-reactid="41">ShaoYang</div>'
pattern=re.compile(r'(?<=<div)*?(?<=>).*?(?=</div>)')
print(re.search(pattern,s))

『肆』 python如何解析html中的轉義字元

用python寫個html的轉義字元轉換的函數,然後調用這個函數進行轉義字元處理即可。

html中的轉義字元並不多。

html常規轉義字元

『伍』 python 怎麼提取html內容啊(正則)

python提取html內容的方法。如下參考:

1.首先,打開Python來定義字元串,在定義的字元串後面加上中括弧,然後在要提取的字元位置輸入。

『陸』 python 去除html標簽的幾種方法

python去除html標簽的幾種方法,代碼如下:

#!/usr/bin/python
#-*-coding:utf-8-*-
'''
Createdon2015-07-08
@author:Administrator
'''
importre

classFilterTag():
def__init__(self):
pass
deffilterHtmlTag(self,htmlStr):
'''
過濾html中的標簽
:paramhtmlStr:html字元串或是網頁源碼
'''
self.htmlStr=htmlStr
#先過濾CDATA
re_cdata=re.compile('//]*//]]>',re.I)#匹配CDATA
re_script=re.compile('<s*script[^>]*>[^<]*<s*/s*scripts*>',re.I)#Script
re_style=re.compile('<s*style[^>]*>[^<]*<s*/s*styles*>',re.I)#style
re_br=re.compile('')#處理換行
re_h=re.compile(']*>')#HTML標簽
re_comment=re.compile('')#HTML注釋
s=re_cdata.sub('',htmlStr)#去掉CDATA
s=re_script.sub('',s)#去掉SCRIPT
s=re_style.sub('',s)#去掉style
s=re_br.sub(' ',s)#將br轉換為換行
blank_line=re.compile(' +')#去掉多餘的空行
s=blank_line.sub(' ',s)
s=re_h.sub('',s)#去掉HTML標簽
s=re_comment.sub('',s)#去掉HTML注釋
#去掉多餘的空行
blank_line=re.compile(' +')
s=blank_line.sub(' ',s)
filterTag=FilterTag()
s=filterTag.replaceCharEntity(s)#替換實體
prints

defreplaceCharEntity(self,htmlStr):
'''
替換html中常用的字元實體
使用正常的字元替換html中特殊的字元實體
可以添加新的字元實體到CHAR_ENTITIES中
CHAR_ENTITIES是一個字典前面是特殊字元實體後面是其對應的正常字元
:paramhtmlStr:
'''
self.htmlStr=htmlStr
CHAR_ENTITIES={'nbsp':'','160':'',
'lt':'<','60':'<',
'gt':'>','62':'>',
'amp':'&','38':'&',
'quot':'"','34':'"',}
re_charEntity=re.compile(r'&#?(?Pw+);')
sz=re_charEntity.search(htmlStr)
whilesz:
entity=sz.group()#entity全稱,如>
key=sz.group('name')#去除&;後的字元如(""--->key="nbsp")去除&;後entity,如>為gt
try:
htmlStr=re_charEntity.sub(CHAR_ENTITIES[key],htmlStr,1)
sz=re_charEntity.search(htmlStr)
exceptKeyError:
#以空串代替
htmlStr=re_charEntity.sub('',htmlStr,1)
sz=re_charEntity.search(htmlStr)
returnhtmlStr

defreplace(self,s,re_exp,repl_string):
returnre_exp.sub(repl_string)


defstrip_tags(self,htmlStr):
'''
使用HTMLParser進行html標簽過濾
:paramhtmlStr:
'''
self.htmlStr=htmlStr
htmlStr=htmlStr.strip()
htmlStr=htmlStr.strip(" ")
result=[]
parser=HTMLParser()
parser.handle_data=result.append
parser.feed(htmlStr)
parser.close()
return''.join(result)

defstripTagSimple(self,htmlStr):
'''
最簡單的過濾html<>標簽的方法注意必須是<任意字元>而不能單純是<>
:paramhtmlStr:
'''
self.htmlStr=htmlStr
#dr=re.compile(r'<[^>]+>',re.S)
dr=re.compile(r']*>',re.S)
htmlStr=re.sub(dr,'',htmlStr)
returnhtmlStr

if__name__=='__main__':
#s=file('Google.html').read()
filters=FilterTag()
printfilters.stripTagSimple("<1>你好")

『柒』 如何用Python爬取出HTML指定標簽內的文本

你好!

可以通過lxml來獲取指定標簽的內容。

#安裝lxml
pipinstalllxml

importrequests
fromlxmlimporthtml

defgetHTMLText(url):
....

etree=html.etree
root=etree.HTML(getHTMLText(url))
#這里得到一個表格內tr的集合
trArr=root.xpath("//div[@class='news-text']/table/tbody/tr");

#循環顯示tr裡面的內容
fortrintrArr:
rank=tr.xpath("./td[1]/text()")[0]
name=tr.xpath("./td[2]/div/text()")[0]
prov=tr.xpath("./td[3]/text()")[0]
strLen=22-len(name.encode('GBK'))+len(name)
print('排名:{:<3},學校名稱:{:<{}} ,省份:{}'.format(rank,name,strLen,prov))

希望對你有幫助!

『捌』 python html字元串怎麼寫

抓網頁數據經常遇到例如>或者 這種HTML轉義符,抓到字元串里很是煩人。
比方說一個從網頁中抓到的字元串
html = '<abc>'

用Python可以這樣處理:
import HTMLParser
html_parser = HTMLParser.HTMLParser()
txt = html_parser.unescape(html) #這樣就得到了txt = '<abc>'

如果還想轉回去,可以這樣:
import cgi
html = cgi.escape(txt) # 這樣又回到了 html = '<abc>'

『玖』 python正則表達式去除html標簽的屬性

importre
test='<pclass="pictext"align="center">陳細妹</p>'
test=re.sub(r'(<[^>s]+)s[^>]+?(>)',r'12',test)
print(test)

『拾』 python 如何過濾 HTML標簽

基於文本文檔(Markdown) 設想好需要的基本需要的表、欄位、類型;
使用 Rails Migration 隨著功能的開發逐內步創建表;
隨著細容節功能的開發、需求,逐步增加欄位,刪除欄位,或者調整欄位類型;
第一個 Release 的時候清理 Migrations 合並成一個;
隨著後期的改動,逐步增加、修改、刪除欄位或表。
基本上我的所有項目都是這么搞的,這和項目是否復雜無關。

閱讀全文

與python過濾html字元相關的資料

熱點內容
化妝品工廠的污水處理 瀏覽:298
魚缸放鹽放到過濾槽 瀏覽:96
什麼品牌凈化器除甲醛效果最好 瀏覽:901
樹脂三乙醇胺硬度 瀏覽:270
燈達樹脂砂輪 瀏覽:296
泰克馬污水提升器無錫 瀏覽:926
水性熱塑性樹脂耐化性 瀏覽:779
史密斯凈化器怎麼重置濾芯 瀏覽:211
濟源年產18萬噸污水處理劑 瀏覽:328
廁所飲水機漏水什麼情況 瀏覽:127
反滲透凈水器如何接燒水壺 瀏覽:262
飲水機為什麼不過濾水 瀏覽:416
海曙污水管道養護單位 瀏覽:431
霧化吸入加入蒸餾水 瀏覽:1
廈門超標排放污水量處罰辦法 瀏覽:782
伊濱區污水處理廠地址 瀏覽:661
純水機進水電磁閥為什麼24v不到位 瀏覽:790
惠而浦直飲凈水機多少錢 瀏覽:40
鍍鉻後的廢水處理最方便 瀏覽:851
既熱式飲水機怎麼裝前置過濾器 瀏覽:585