Python使用BeautifulSoup4修改網(wǎng)頁內(nèi)容的實(shí)戰(zhàn)記錄
最近有個(gè)小項(xiàng)目,需要爬取頁面上相應(yīng)的資源數(shù)據(jù)后,保存到本地,然后將原始的HTML源文件保存下來,對(duì)HTML頁面的內(nèi)容進(jìn)行修改將某些標(biāo)簽整個(gè)給替換掉。
對(duì)于這類需要對(duì)HTML進(jìn)行操作的需求,最方便的莫過于 BeautifulSoup4 的庫了。
樣例的HTML代碼如下:
<html> <body> <a class="videoslide" rel="external nofollow" rel="external nofollow" > <img src="http://www.test.com/wp-content/uploads/1020/1381824922_zy_compress.JPG" data-zy-media-id="zy_location_201310151613422786"/> </a> <a rel="external nofollow" rel="external nofollow" > <img data-zy-media-id="zy_image_201310151613169945" src="http://www.test.com/wp-content/uploads/1020/第一張_1381824798_zy_compress.JPG"/></a> <a rel="external nofollow" rel="external nofollow" > <img data-zy-media-id="zy_image_201310151613163009" src="http://www.test.com/wp-content/uploads/1020/第二張_1381824796_zy_compress.jpg"/> </a> <a rel="external nofollow" rel="external nofollow" > <img data-zy-media-id="zy_image_201312311838584446" src="http://www.test.com/wp-content/uploads/1020/第三張_zy_compress.jpg"/> </a> </body> </html>
這里主要包括了 <a >
標(biāo)簽, <a >
標(biāo)簽里面嵌入了 <img >
標(biāo)簽,其中有 <a class="videoslide">
的標(biāo)識(shí)該標(biāo)簽實(shí)際是可以播放動(dòng)畫的。需要根據(jù) class="videoslide"
來判斷將整個(gè) <a >
標(biāo)簽換成播放器的 <video >
標(biāo)簽,將沒有 class="videoslide"
的 <a >
標(biāo)簽換成 <figure>
標(biāo)簽。
也就是將帶有的 <a class="videoslide" ...><img ... /></a>
標(biāo)簽換成
<div class="video"> <video controls width="100%" poster="視頻鏈接的圖片地址.jpg"> <source src="視頻文件的靜態(tài)地址.mp4" type="video/mp4" /> 您的瀏覽器不支持H5視頻,請(qǐng)使用Chrome/Firefox/Edge瀏覽器。 </video> </div>
將 <a ....><img .../></a>
標(biāo)簽換成
<figure> < img src="圖片地址_compressed.jpg" data-zy-media-id="圖片地址.jpg"> <figcaption>文字說明(如果有)</figcaption> </figure>
這里通過BeautifulSoup4 的select()方法找到標(biāo)簽,通過get()方法獲取標(biāo)簽及標(biāo)簽屬性值,通過replaceWith來替換標(biāo)簽,具體代碼如下:
首先安裝BeautifulSoup4的庫,BeautifulSoup4庫依賴于lxml庫,所以也需要安裝lxml庫。
pip install bs4 pip install lxml
具體代碼實(shí)現(xiàn)如下:
import os from bs4 import BeautifulSoup htmlstr='<html><body>' \ '<a class="videoslide" rel="external nofollow" rel="external nofollow" >' \ '<img src="http://www.test.com/wp-content/uploads/1020/1381824922_zy_compress.JPG" data-zy-media-id="zy_location_201310151613422786"/></a>' \ '<a rel="external nofollow" rel="external nofollow" >' \ '<img data-zy-media-id="zy_image_201310151613169945" src="http://www.test.com/wp-content/uploads/1020/第一張_1381824798_zy_compress.JPG"/></a>' \ '<a rel="external nofollow" rel="external nofollow" >' \ '<img data-zy-media-id="zy_image_201310151613163009" src="http://www.test.com/wp-content/uploads/1020/第二張_1381824796_zy_compress.jpg"/></a>' \ '<a rel="external nofollow" rel="external nofollow" >' \ '<img data-zy-media-id="zy_image_201312311838584446" src="http://www.test.com/wp-content/uploads/1020/第三張_zy_compress.jpg"/></a>' \ '</body></html>' def procHtml(htmlstr): soup = BeautifulSoup(htmlstr, 'lxml') a_tags=soup.select('a') for a_tag in a_tags: a_tag_src = a_tag.get('href') a_tag_filename = os.path.basename(a_tag_src) a_tag_path = os.path.join('src', a_tag_filename) a_tag['href']=a_tag_path next_tag=a_tag.next #判斷是視頻還是圖片,如果a標(biāo)簽帶了class="videoslide" 是視頻否則是圖片 if a_tag.get('class') and 'videoslide'==a_tag.get('class')[0]: # 處理視頻文件 media_id = next_tag.get('data-zy-media-id') if media_id: media_url = 'http://www.test.com/travel/show_media/' + str(media_id)+'.mp4' media_filename = os.path.basename(media_url) media_path = os.path.join('src', media_filename) # 將div.video標(biāo)簽替換a標(biāo)簽 video_html = '<div class=\"video\"><video controls width = \"100%\" poster = \"' + a_tag_path + '\" ><source src = \"' + media_path + '\" type = \"video/mp4\" /> 您的瀏覽器不支持H5視頻,請(qǐng)使用Chrome / Firefox / Edge瀏覽器。 </video></div>' video_soup = BeautifulSoup(video_html, 'lxml') a_tag.replaceWith(video_soup.div) else: #獲取圖片信息 if 'img'==next_tag.name: img_src=next_tag.get('src') # 判斷是否路徑是否為本地資源 data:image和file: if img_src.find('data:image') == -1 and img_src.find('file:') == -1: img_filename = os.path.basename(img_src) img_path = os.path.join('src', img_filename) # 將<figure><img>標(biāo)簽替換a標(biāo)簽 figcaption='' figure_html='<figure><img src=\"'+img_path+'\" data-zy-media-id=\"'+a_tag_path+'\"><figcaption>'+figcaption+'</figcaption></figure>' figure_soup = BeautifulSoup(figure_html, 'lxml') a_tag.replaceWith(figure_soup.figure) html_content = soup.contents[0] return html_content if __name__ == '__main__': pro_html_str=procHtml(htmlstr) print(pro_html_str)
結(jié)果:
<html> <body> <div class="video"> <video controls="" poster="src\1381824922.JPG" width="100%"> <source src="src\zy_location_201310151613422786.mp4" type="video/mp4"/> 您的瀏覽器不支持H5視頻,請(qǐng)使用Chrome / Firefox / Edge瀏覽器。 </video> </div> <figure> <img data-zy-media-id="src\第一張_1381824798.JPG" src="src\第一張_1381824798_zy_compress.JPG"/> <figcaption></figcaption> </figure> <figure> <img data-zy-media-id="src\第二張_1381824796.jpg" src="src\第二張_1381824796_zy_compress.jpg"/> <figcaption></figcaption></figure> <figure> <img data-zy-media-id="src\第三張.jpg" src="src\第三張_zy_compress.jpg"/> <figcaption></figcaption> </figure> </body> </html>
總結(jié)
到此這篇關(guān)于Python使用BeautifulSoup4修改網(wǎng)頁內(nèi)容的文章就介紹到這了,更多相關(guān)Python BeautifulSoup4修改網(wǎng)頁內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)bilibili時(shí)間長(zhǎng)度查詢的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)bilibili時(shí)間長(zhǎng)度查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python關(guān)于excel和shp的使用在matplotlib
今天小編就為大家分享一篇關(guān)于Python關(guān)于excel和shp的使用在matplotlib,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01Python實(shí)現(xiàn)矩陣加法和乘法的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)矩陣加法和乘法的方法,結(jié)合實(shí)例形式對(duì)比分析了Python針對(duì)矩陣的加法與乘法運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-12-12