日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python读取html中body_python批量获取html内body内容的实例

發布時間:2025/3/15 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取html中body_python批量获取html内body内容的实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

# -*- coding:utf8 -*-

from bs4 import BeautifulSoup

import os

import os.path

import sys

reload(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內容的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

總結

以上是生活随笔為你收集整理的python读取html中body_python批量获取html内body内容的实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。