首页 > 编程 > Python > 正文

python 解析html之BeautifulSoup

2020-02-23 04:44:57
字体:
来源:转载
供稿:网友
代码如下:
# coding=utf-8
from BeautifulSoup import BeautifulSoup, Tag, NavigableString
from SentenceSpliter import SentenceSpliter
from os.path import basename,dirname,isdir,isfile
from os import makedirs
from shutil import copyfile
import io
import time
import re

class build_tpl:
    def __init__(self,parse_file,build_tpl_name,cp_pic_dir,show_pic_dir,js_path,set_lang=2052):
        '''参数说明:解析文件名,模版名称,保存图片路径,图片显示路径,js路径,当前语言(分句使用)'''

        #取得解析文件目录路径
        if len(dirname(parse_file))>1:
            self.cur_dir = dirname(parse_file)+"/";
        else:
            self.cur_dir ="./";

        #建立的模版文件文件名
        self.build_tpl_name = build_tpl_name;
        #图片cp到得目录
        self.cp_pic_dir = cp_pic_dir;
        #通过http展现图片的目录
        self.show_pic_dir = show_pic_dir;
        #加载js的路径
        self.js_path = js_path;

        #句段组
        self.get_text_arr = [];
        #当前图片名数组
        self.cur_pic_arr = [];

        #解析文件 取得soup 资源
        self.soup = self.get_soup(parse_file);
        #取得html文档中,段文档
        self.get_text_arr = self.soup.body.findAll(text=lambda(x): len(x.strip()) > 0);
        #取得句对
        self.get_sentence_arr = self.parse_text(self.get_text_arr,set_lang);
        #取得替换数组
        self.replace_list = self.get_replace_list(self.get_text_arr,set_lang);
        #取得图片数组
        self.cur_pic_arr = self.soup.findAll('img');

        #self.write_file_by_list("no.txt",self.get_text_arr);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表