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

python3過濾html

發布時間:2021-01-29 16:36:47

❶ python 如何過濾 HTML標簽

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

❷ python3爬蟲獲取HTML文檔時的問題。

很正常。控制台支持的編碼有限。建議你存到文件再打開看是否正常。
另外頁面寫了是gbk編碼,存文件也試試gbk

❸ python3.x提取網頁全部文本(要求適用全部網站)

#coding=utf-8
importrequests
importsys

defgetHtmlText(url,prefix='http'):
ifnoturl.startswith(prefix):
url='{}://{}'.format(prefix,url)
try:
kv={'User-Agent':'Mozilla/5.0'}#模擬瀏覽器Mozilla
r=requests.get(url,headers=kv,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
returnr.text
except:
return'爬取失敗,請檢查網址或網路連接'

if__name__=='__main__':
url='www..com'#要爬取的網站
iflen(sys.argv)>1:
url=sys.argv[1]
fname='a.html'#爬取結果寫在這個文件里
iflen(sys.argv)>2:
fname=sys.argv[2]
text=getHtmlText(url)
withopen(fname,'w')asf:
f.write(text)
print('contentofsitewriteat"',fname,'"withlengthof',len(text))

ref:http://www.icourse163.org/course/BIT-1001870001

❹ 求教python 網站爬蟲過濾出圖片 url 的問題

  1. lxml

#!/usr/bin/python3.4
#-*-coding:utf-8-*-
#教程:http://www.cnblogs.com/TTyb/p/5832790.html
fromlxmlimportetree
importurllib.request

#目標網址的html可以看一下
url="http://www.1kkk.com/manhua589/"
#解析網址
data=urllib.request.urlopen(url).read()
#解碼
html=data.decode('UTF-8','ignore')

page=etree.HTML(html.lower())

#查找的目標樣式如下
"""
...
<ulclass="sy_nr1cplist_ullg">
<li>
<ahref="/vol1-6871/"class="tg">第1卷</a>(96頁)</li>
<li>
<ahref="/vol2-6872/"class="tg">第2卷</a>(90頁)</li>
<li>
<ahref="/vol3-6873/"class="tg">第3卷</a>(95頁)</li>
<li>
<ahref="/vol4-6874/"class="tg">第4卷</a>(94頁)</li>
<li>
<ahref="/vol5-6875/"class="tg">第5卷</a>(95頁)</li>
...
"""

#找到ul下li下的a中的href
hrefs=page.xpath('//ul[@class="sy_nr1cplist_ullg"][2]/li/a/@href')

#找到<a>...</a>之間的文字
hrefnames=page.xpath('//ul[@class="sy_nr1cplist_ullg"][2]/li/a/text()')

#找到頁數
hrefpages=page.xpath('//ul[@class="sy_nr1cplist_ullg"][2]/li/text()')

forhrefinhrefs:
#列印出來
print(href)

2.BS4

#!/usr/bin/python3.4
#-*-coding:utf-8-*-
importurllib.request
frombs4importBeautifulSoup
#教程:http://www.cnblogs.com/TTyb/p/5799564.html
if__name__=='__main__':
url="http://www.lenggirl.com/"
headers={
'User-Agent':'Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.11(KHTML,likeGecko)Chrome/23.0.1271.64Safari/537.11',
'Accept':'text/html;q=0.9,*/*;q=0.8',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding':'gzip',
'Connection':'close',
'Referer':None
}
data=urllib.request.urlopen(url).read()
#('UTF-8')('unicode_escape')('gbk','ignore')
data=data.decode('UTF-8','ignore')
#初始化網頁
soup=BeautifulSoup(data,"html.parser")
#列印整個網頁
html=soup.prettify()
#列印<head>...</head>
head=soup.head
#列印<body>...</body>
body=soup.body
#列印第一個<p>...</p>
p=soup.p
#列印p的內容
p_string=soup.p.string
#soup.p.contents[0]為Aug22,2016
#soup.p.contents為['Aug22,2016 ']
p_string=soup.p.contents[0]
#將body裡面的所有頭列印出來
forchildinsoup.body.children:
#print(child)
pass
#將所有的<a>...</a>和<p>...</p>列印出來
a_and_p=soup.find_all(["a","p"])
#找到<a>...</a>下所有的網址
formyimginsoup.find_all('a'):
img_src=myimg.get('href')
#print(img_src)
#找到<a>...</a>下類為class_='a'下面的<img>...</img>裡面的src
formyimginsoup.find_all('a',class_='a'):
img_src=myimg.find('img').get('src')
#網頁所有信息
#print(html)

3.正則

❺ python 正則 匹配HTML

正確的復html閉標簽里,是以制/開頭的,所以網頁的源代碼不可能是<\/span>,<span class=\"name\">小小少年lala<\/span>\r\n\t里的前三個\是為了在字元串里顯示「和/而已,並不是字元串的內容。

網頁應該是
<span class="name">小小少年lala</span>\r\n\t

你的正則應該用
p=re.compile('<span class="name">(.*)<\/span>')

❻ python3 能解析html嗎

可以的,使用beautifulsoup就可以解析了。
import urllib
import urllib.request
import beautifulsoup
html = urllib.request.urlopen('http://yugioh.wikia.com/wiki/Card_Tips:Blue-Eyes_White_Dragon').read()
soup = beautifulsoup.bs4(html)
texts = soup.findAll(text=True)
def visible(element):
if element.parent.name in ['style', 'script', '[document]', 'head', 'title']:
return False
elif re.match('<!--.*-->', str(element)):
return False
return True
visible_texts = filter(visible, texts)

❼ python正則表達式去除html標簽的屬性

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

❽ python3 htmltestrunner 怎麼判斷用例失敗

修改HTMLTestRunner.py以支持+

搜索到的結果整理

❾ 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標簽

可以使用正則表達式的方法

正則表達式:工作職責:</th>s+<td>(.+?)</td>


importre
content="頁面內容"
re_1=re.search('工作職責:</th>s+<td>(.+?)</td>',content)
ifre_1:
printre_1.group(1)
else:
print"notfind!"

因為正則表達式有中文 所以要保證你的內容與文本是一個編碼

閱讀全文

與python3過濾html相關的資料

熱點內容
純化水純水箱怎麼清洗 瀏覽:236
純水為什麼要用不銹鋼 瀏覽:98
樹脂膠表帶黑變黑了 瀏覽:829
超濾飲水機和反滲透的區別 瀏覽:181
618凈水器怎麼選 瀏覽:585
染料化工廢水a2o停留時間 瀏覽:132
飲水機凍水了怎麼辦 瀏覽:124
家用水箱濾芯怎麼卸 瀏覽:271
上溢流過濾溢水 瀏覽:879
億心源家用凈水器怎麼樣 瀏覽:892
兩批櫃機不排廢水 瀏覽:693
電熱水器白醋除垢 瀏覽:855
保安過濾器招標 瀏覽:753
污水廠偷排怎麼舉報 瀏覽:786
凈水器tds值偏高怎麼回事 瀏覽:615
廣東省uv樹脂生產企業 瀏覽:564
沈陽除垢公司 瀏覽:771
玻璃鋼防腐用哪種樹脂 瀏覽:223
高中化學蒸餾板書 瀏覽:568
12年甲殼蟲空調濾芯怎麼更換 瀏覽:356