怎么用python把歌词内

Python022

怎么用python把歌词内,第1张

from urllib.request import urlopenfrom bs4 import BeautifulSoupimport reimport numpyimport csv

starturl="http://www.cnlyric.com/geshou/1927.html" # 凤凰传奇歌词地址第一页# 找出下一页链接def findnextlinks(starturl,nextlinks):

    """ 该函数用于从starturl页面开始,递归找出所有“下一页”的链接地址

    要求nextlinks为一个空的列表"""

    try:

        html=urlopen(starturl)

        bsobj=BeautifulSoup(html,"lxml")

        nextpagelink=bsobj.find("div",{"class":"PageList"}).input.\

        previous_sibling.previous_sibling.attrs["href"]

        nextlink="http://www.cnlyric.com/geshou/"+nextpagelink

        nextlinks.append(nextlink)

        findnextlinks(nextlink,nextlinks)    except:

        print("\n所有“下一页”的链接寻找完毕")    return nextlinks

nextlinks=[]

nextlinks=findnextlinks(starturl,nextlinks) # 所有“下一页”的链接列表# 找出存放歌词的链接列表def findlrclinks(urllists):

    """ 该函数用于找出列表urllists中的链接页面上存放歌词的链接 """

    Sites=[]    for urllist in urllists:

        html=urlopen(urllist)

        bsobj=BeautifulSoup(html,"lxml")        for link in bsobj.findAll(href=re.compile("^(../LrcXML/)")):

            site="http://www.cnlyric.com"+link.attrs["href"].lstrip("..")

            Sites.append(site)    return Sites

nextlinks.insert(0,starturl) # 将开始页面也加入链接列表中Sites=findlrclinks(nextlinks) # 找出所有存放歌词的链接print("\n所有曲目歌词所在的xml文件链接寻找完毕")def getlrc(lrclink):

    """ 该函数用于找出歌词链接lrclink中的歌词,并以列表形式保存 """

    LRC=[]

    html=urlopen(lrclink)

    bsobj=BeautifulSoup(html,"lxml")

    lrcpre=bsobj.findAll("lrc")    for lrclabel in lrcpre:

        lrc=lrclabel.get_text()

        LRC.append(lrc)    return LRC

csvfile=open("凤凰传奇歌词集.csv","w+") # 创建csv文件用于保存数据try:

    writer=csv.writer(csvfile)

    rowindex=1

    for lrcurl in Sites:

        LRC=getlrc(lrcurl)

        LRC.insert(0,str(rowindex).zfill(3))

        writer.writerow(LRC) # 将每首哥编号并将歌词写入从中文件中

        rowindex+=1finally:

    csvfile.close() # 关闭csv文件

12345ph = float(raw_input("give a number:"))if ph <7.0:print "%s is acidic."%(ph)if ph <4.0:print "%s is VERY acidic!Be careful."%(ph)

就做一下标点符号的替换吧

txt = open("绝代风华.txt","r+",encoding='utf-8').read()#修改访问模式为"r+"

txt2=txt.replace(",",' ').replace("。",' ')#还有什么符号就自己加吧

txt3=open("E://绝代风华2.txt","w+",encoding='utf-8')#设置路径比较好,在文件名前加

txt3.write(txt2)#写入替换好的文本,形成新文本

#以下三句放在代码的后面

txt.close()

txt3.close()

#打开新文本,若查看的文本内容无误,这两行代码可以注释。

pl=open("E://绝代风华2.txt","r",encoding='utf-8').read()

pl.close()