一个完整的大作业

 

一个完整的大作业

1.选一个自己感兴趣的主题。

2.网络上爬取相关的数据。

3.进行文本分析,生成词云。

4.对文本分析结果解释说明。

5.写一篇完整的博客,附上源代码、数据爬取及分析结果,形成一个可展示的成果。

 

1.选一个自己感兴趣的主题。

一个完整的大作业_第1张图片

 

2.网络上爬取相关的数据。

import requests
from bs4 import BeautifulSoup
import pandas
import sqlite3
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt

def songdetail(url):
    a=requests.get(url)
    a.encoding='utf-8'
    ai=BeautifulSoup(a.text,'html.parser')
    we={}
    we['songname']=ai.select('.name ')[0].text#歌名
    we['url']=url#歌曲链接
    we['player']=ai.select('.base-info > li > span > a')[0].text#歌手
    return(we)
#print(songdetail('http://music.baidu.com/song/274841326'))#一首歌曲的详细内容
def allsong(songurl):
    b=requests.get(songurl)
    b.encoding='utf-8'
    ei=BeautifulSoup(b.text,'html.parser')
    he=[]
    for s in ei.select('.song-title'):
        if len(s.select('a')) > 0:
            he.append(songdetail('http://music.baidu.com'+s.select('a')[0]['href']))
    return(he)
#print(allsong('http://music.baidu.com/top/dayhot'))#Top500中所有歌曲条目
songstotal=[]
aurl='http://music.baidu.com/top/dayhot'
songstotal.extend(allsong(aurl))#把曲目放入列表
#print(songstotal)
'''
ki=pandas.DataFrame(songstotal)

ki.to_excel('song.xlsx')#生成Excel文件
with sqlite3.connect('song.sqlite') as db:
    ki.to_sql('song',con=db)#生成数据库文件
'''
for i in range(499):
    if i>0:
        name=name+songstotal[i].get('player')
    else:
        name=songstotal[i].get('player')#返回所有歌手姓名的文本

for i in ',。:?!“”() ':
    named=name.replace(i,'')
fi=list(jieba.cut(named))#分词形成列表
#print(fi)

q={}
hc=set(fi)
for i in hc:
    q[i]=fi.count(i)
eu=list(q.items())
eu.sort(key=lambda x:x[1],reverse=True)#字典返回列表并排序
#print(eu)
mc=WordCloud().generate_from_frequencies(q)    
plt.imshow(mc)
plt.show()#生成词云

 一个完整的大作业_第2张图片

一个完整的大作业_第3张图片

 

3.进行文本分析,生成词云。

 一个完整的大作业_第4张图片

 

 

转载于:https://www.cnblogs.com/OKding/p/7763423.html

你可能感兴趣的:(一个完整的大作业)