国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python批量獲取html內body內容的實例

2020-02-16 00:26:55
字體:
來源:轉載
供稿:網友

現在有一批完整的關于介紹城市美食、景點等的html頁面,需要將里面body的內容提取出來

方法:利用python插件beautifulSoup獲取htmlbody標簽的內容,并批量處理。

# -*- coding:utf8 -*- from bs4 import BeautifulSoupimport osimport os.pathimport sysreload(sys) sys.setdefaultencoding('utf8')   def printPath(level,path):	global allFileNum	#所有文件夾,第一個字段是此目錄的級別	dirList = [] 	#所有文件	fileList = [] 	#返回一個列表,其中包含在目錄條目的名稱	files = os.listdir(path) 	#先添加目錄級別	dirList.append(str(level)) 	for f in files:		if(os.path.isdir(path+'/'+f)):			#排除隱藏文件夾,因為隱藏文件夾過多			if(f[0] == '.'):				pass			else:				#添加隱藏文件夾				dirList.append(f)		if(os.path.isfile(path+'/'+f)):			#添加文件			fileList.append(f)	return (dirList,fileList) #將文件html文件抓取并寫入指定txt文件def getAndInsert(rootdir,savepath,path):	global file_num	f_list = os.listdir(rootdir+'/'+path)	for i in f_list:		temp = os.path.splitext(i)[0]		for num in range(1,11):			if(i==str(num)+'.html'):				#print rootdir+'/'+path+'/'+i				objFile = open(rootdir+'/'+path+'/'+i)				soup = BeautifulSoup(objFile)				arr = []				for child in soup.body:					arr.append(child)				if os.path.exists(savepath+'/'+path):					pass				else:					os.makedirs(savepath+'/'+path)				f = open(savepath+'/'+path+'/'+temp+'.txt','w')				for k,v in enumerate(arr):					if k!=1:						f.write(str(v))				f.close()				print path+'/'+i+' is running'	file_num = file_num + 1			 rootdir = '../zips2'dirList,fileList = printPath(1,rootdir) savepath = "../testC"file_num = 0 for fn in dirList:	if(fn == '1'):		pass	else:		getAndInsert(rootdir,savepath,fn)		print fn+' is ending'print '一共完成'+str(file_num)+'個城市的提取'

以上這篇python批量獲取html內body內容的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景东| 都匀市| 丽江市| 皮山县| 泾源县| 闽清县| 浦城县| 光山县| 丰县| 米泉市| 子洲县| 嘉荫县| 定结县| 同江市| 浙江省| 包头市| 嫩江县| 龙泉市| 右玉县| 隆化县| 承德县| 重庆市| 廉江市| 沁阳市| 德州市| 滦平县| 鄂州市| 鹿泉市| 东莞市| 岢岚县| 广州市| 克拉玛依市| 太湖县| 永泰县| 上饶县| 永修县| 宜川县| 酒泉市| 垫江县| 航空| 荃湾区|