python批量获取html内body内容
来源:互联网 发布:新纳粹知乎 编辑:程序博客网 时间:2024/05/22 15:09
现在有一批完整的关于介绍城市美食、景点等的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] == '.'):passelse:#添加隐藏文件夹dirList.append(f)if(os.path.isfile(path+'/'+f)):#添加文件fileList.append(f)return (dirList,fileList)#将文件html文件抓取并写入指定txt文件def getAndInsert(rootdir,savepath,path):global file_numf_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+'/'+iobjFile = open(rootdir+'/'+path+'/'+i)soup = BeautifulSoup(objFile)arr = []for child in soup.body:arr.append(child)if os.path.exists(savepath+'/'+path):passelse: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 + 1rootdir = '../zips2'dirList,fileList = printPath(1,rootdir)savepath = "../testC"file_num = 0for fn in dirList:if(fn == '1'):passelse:getAndInsert(rootdir,savepath,fn)print fn+' is ending'print '一共完成'+str(file_num)+'个城市的提取'
阅读全文
0 0
- python批量获取html内body内容
- java获取HTML纯文本内容 body
- 正则获取HTML标签内的内容
- 我的html-body内的标签
- 利用webBrowser获取框架内Html页面内容
- 利用webBrowser获取框架内Html页面内容
- 利用webBrowser获取框架内Html页面内容
- C# webBrowser 获取框架内Html页面内容
- HTML·设置body中内容居中
- beego 获取 Request Body 里的内容
- js 获取html table单元格内容,获取单元格内输入框中的内容
- 获取html网页中的body部分内容
- 【Java】获取指定HTML 文档指定的body、页中超链接的标题和链接、指定博客文章的内容
- Document 对象属性和方法:例如document.scripts...直接获取html中包括head和body里面的内容
- Python进阶_2.通过URL获取HTMl内容
- Python获取网页内容、使用BeautifulSoup库分析html
- sqlserver 批量获取proc内容
- 获取HTML表格内容
- socket()
- java常量池和运行时常量池
- JS学习之数组;
- 9.8.3 消除消失的编号
- 中国性别平等,要等到卫生棉条普及的那一天
- python批量获取html内body内容
- 获取安卓设备UUID
- 项目整理
- Apache的ActiveMQ
- Android开发之shap
- ubuntu装nvidia驱动,CUDA的血泪
- 使用Spring创建对象的方式
- js 数组的应用
- libpng warning: Application built with libpng-1.6.27 but running with 1.5.13