簡繁體轉換

      在〈簡繁體轉換〉中尚無留言

所需套件

簡繁轉換, 需先下載如下二個套件
langconv.py 及 zh_wiki.py

trans套件, 是將中文的五百三十五轉換成 535,請由如連結下載
zh_number.py

import os
import requests, re
import codecs
from bs4 import BeautifulSoup
from langconv import Converter
from zh_number import trans

page = requests.get("http://www.uuxs.tw/ls/22_22102/")
page.encoding="utf-8"
soup=BeautifulSoup(page.text, "html.parser")
print(soup.prettify())
pattern=re.compile('.*第.*章')
chapterList=soup.find_all('a', string=pattern)

urlno=[]
for l in chapterList:
    urlno.append(int((l['href'].replace('.html',''))))
urlno.sort()

path = "d:/天降巨富"
if not os.path.isdir(path):
    os.mkdir(path)

for no in urlno:
    url="http://www.uuxs.tw/ls/22_22102/{0}.html".format(no)
    page=requests.get(url)
    page.encoding="utf-8"
    soup = BeautifulSoup(page.text, 'html.parser')
    cnChapter = soup.find(attrs={"class": "bookname"}).h1
    content = soup.find(id='content')
    zhChapter=Converter('zh-hant').convert(cnChapter.string).replace(' ','')
    zh_num=zhChapter.split('章')[0].replace('第', '')
    num=trans(zh_num)
    zh_title=zhChapter.split('章')[1]
    fileName="d:/天降巨富/No%03d.%s.txt" % (num, zh_title)
    file = codecs.open(fileName, 'w', 'utf-8')
    print(fileName)
    file.write(zhChapter)
    for s in content.strings:
        zhContent=Converter('zh-hant').convert(s)
        file.write(zhContent)
    file.close()

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *