欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Python培訓(xùn)簡(jiǎn)述Python批量獲取html內(nèi)body內(nèi)容的方法

2019-05-23 14:21:09 4735瀏覽

小伙伴們大家好,本篇文章扣丁學(xué)堂Python培訓(xùn)小編給讀者們分享一下Python批量獲取html內(nèi)body內(nèi)容的實(shí)例,文中有詳細(xì)的代碼列出供小伙伴們參考,感興趣的小伙伴就隨小編來(lái)了解一下吧。

扣丁學(xué)堂Python培訓(xùn)簡(jiǎn)述Python批量獲取html內(nèi)body內(nèi)容的方法

現(xiàn)在有一批完整的關(guān)于介紹城市美食、景點(diǎn)等的html頁(yè)面,需要將里面body的內(nèi)容提取出來(lái)。


方法:利用python插件beautifulSoup獲取htmlbody標(biāo)簽的內(nèi)容,并批量處理。


# -*- coding:utf8 -*-
 
from bs4 import BeautifulSoup
import os
import os.path
import sys
reload(sys) 
sys.setdefaultencoding('utf8') 
 
 
def printPath(level,path):
	global allFileNum
	#所有文件夾,第一個(gè)字段是此目錄的級(jí)別
	dirList = []
 
	#所有文件
	fileList = []
 
	#返回一個(gè)列表,其中包含在目錄條目的名稱
	files = os.listdir(path)
 
	#先添加目錄級(jí)別
	dirList.append(str(level))
 
	for f in files:
		if(os.path.isdir(path+'/'+f)):
			#排除隱藏文件夾,因?yàn)殡[藏文件夾過(guò)多
			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)+'個(gè)城市的提取'

最后想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺(tái)為您提供權(quán)威的Python開發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無(wú)限,行業(yè)薪資和未來(lái)的發(fā)展會(huì)越來(lái)越好的,扣丁學(xué)堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Python技術(shù)交流群:279521237。

扣丁學(xué)堂微信公眾號(hào)                          Python全棧開發(fā)爬蟲人工智能機(jī)器學(xué)習(xí)數(shù)據(jù)分析免費(fèi)公開課直播間


      【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】         【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】



查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>


標(biāo)簽: Python培訓(xùn) Python視頻教程 Python在線視頻 Python學(xué)習(xí)視頻 Python培訓(xùn)班

熱門專區(qū)

暫無(wú)熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部