導航:首頁 > 凈水問答 > python3過濾標簽

python3過濾標簽

發布時間:2023-04-16 22:00:19

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

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

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

Ⅱ Python3.6.3 中BeautifSoup過濾標簽中的文本

直接span.string就可以取出代碼里的字元串,包括中文

你在for循環那裡,最後兩行去掉,用print(six.string)代替就行

Ⅲ python 怎麼過濾特殊字元

#coding:utf-8
defcolate(st="你要過濾的字元串",ch='你要過濾的特殊字元'):
return''.join(st.split(ch))
#如果要過濾多個特殊字元的話,可以多次調用這個函數

Ⅳ python 爬蟲怎麼過濾正文以外的

利用bs4查找所有的div,用正則篩選出每個div裡面的中文,找到中文字數最多的div就是屬於正文的div了。定義一個抓取的頭部抓取網頁內容:

importrequests
headers={
'User-Agent':'Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/47.0.2526.106Safari/537.36',
'Host':'blog.csdn.net'}
session=requests.session()

defgetHtmlByRequests(url):
headers.update(
dict(Referer=url,Accept="*/*",Connection="keep-alive"))
htmlContent=session.get(url=url,headers=headers).content
returnhtmlContent.decode("utf-8","ignore")

統計文字的正則:

importre
#統計中文字數
defcountContent(string):
pattern=re.compile(u'[u1100-uFFFD]+?')
content=pattern.findall(string)
returncontent

查找每一個div,統計每一個div的文字,只保留文字最多的那個div:

#分析頁面信息
defanalyzeHtml(html):
#初始化網頁
soup=BeautifulSoup(html,"html.parser")
part=soup.select('div')
match=""
forparagraphinpart:
content=countContent(str(paragraph))
iflen(content)>len(match):
match=str(paragraph)
returnmatch

最後的調用幾個函數即可:

defmain():
url="http://blog.csdn.net/"
html=getHtmlByRequests(url)
mainContent=analyzeHtml(html)
soup=BeautifulSoup(mainContent,"html.parser")
print(soup.select('div')[0].text)

Ⅳ 怎樣用正則表達式過濾掉頁面中除了<p></p>和<img>以外所有的標簽

這個還真不容易實現,單獨保留p或者img都可以,但是兩個條件放一起就不行了。於專是我換屬了一種思路,用了個函數實現了,你看下,代碼是python下的:

importre

t='<html>asdfasdf<head>1111111111<body><p>asdfasdfasdf</p><imgherf="fff">'
defreplace_two(m):
"""
#過濾掉頁面中除了<p></p>和<img>以外所有的標簽
"""
all=re.findall(r'</?.*?>',m)
save=re.findall(r'</?(?:img).*?>|</?[pP]*?>',m)

foreinall:
ifenotinsave:
m1=m.replace(e,'')
m=m1
returnm

printreplace_two(t)

Ⅵ 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>你好")

Ⅶ python3怎樣過濾字元串中的表情

importre

emoji_pattern=re.compile(
u"(ud83d[ude00-ude4f])|"#emoticons
u"(ud83c[udf00-uffff])|"#symbols&pictographs(1of2)
u"(ud83d[u0000-uddff])|"#symbols&pictographs(2of2)
u"(ud83d[ude80-udeff])|"#transport&mapsymbols
u"(ud83c[udde0-uddff])"#flags(iOS)
"+",flags=re.UNICODE)defremove_emoji(text):
returnemoji_pattern.sub(r'',text)

來自:http://blog.csdn.net/orangleliu/article/details/67632628?utm_source=gold_browser_extension

上面那個有時不好用,

try:
#pythonUCS-4build的處理方式
highpoints=re.compile(u'[U00010000-U0010ffff]')
exceptre.error:
#pythonUCS-2build的處理方式
highpoints=re.compile(u'[uD800-uDBFF][uDC00-uDFFF]')

resovle_value=highpoints.sub(u'??',src_string)

嘗試一下這個。

閱讀全文

與python3過濾標簽相關的資料

熱點內容
聊城反滲透膜清洗 瀏覽:58
保山礦井污水處理設備多少錢 瀏覽:734
空氣過濾減壓閥怎樣 瀏覽:606
斗山回油濾芯上面裝的小碗是什麼 瀏覽:187
蒸餾裝置中的前餾分是什麼 瀏覽:275
污水用日語怎麼說 瀏覽:507
大眾2016polo換空氣濾芯怎麼換 瀏覽:697
water是什麼牌子的凈水器 瀏覽:560
雷沃鏟車液壓油箱怎麼換濾芯 瀏覽:897
蒸餾裝置中如何正確安裝溫度計 瀏覽:381
車用液壓機油濾芯怎麼清洗 瀏覽:437
納米水性環氧樹脂 瀏覽:296
凈化器定時h是什麼意思 瀏覽:313
一體式濾芯買什麼牌子的好 瀏覽:314
釀造酒與蒸餾酒成分比較 瀏覽:140
電熱水器排污水開關壞 瀏覽:177
污水廠排放量計算 瀏覽:325
飲水機冷膽漏水什麼原因 瀏覽:57
陶氏超濾膜2880過濾面積 瀏覽:823
沃康延齡泉飲水機怎麼樣 瀏覽:830