tushare pro接口_利用tushare获取新闻联播文字稿并制作词云
生活随笔
收集整理的這篇文章主要介紹了
tushare pro接口_利用tushare获取新闻联播文字稿并制作词云
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
安裝
首先需要一堆python的第三方庫(kù),俺用的是大清的鏡像,一點(diǎn)都不卡哦。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jieba pip install -i https://pypi.tuna.tsinghua.edu.cn/simple wordcloud pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tushare pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl pip install -i https://pypi.tuna.tsinghua.edu.cn/simple datetime pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio文字稿件的獲取
俺是從tushare金融社區(qū)獲得的文字稿,這是網(wǎng)址:https://tushare.pro/document/2?doc_id=154。
此外,你需要注冊(cè)一個(gè)賬號(hào),在經(jīng)過實(shí)名驗(yàn)證之后獲得20個(gè)積分,就可以調(diào)取數(shù)據(jù)了。
代碼
import wordcloud import jieba import openpyxl from imageio import imread import datetime import tushare as ts import time c=openpyxl.Workbook()#創(chuàng)建一個(gè)工作簿 b=c.active#創(chuàng)建一個(gè)工作表 ts.set_token('token號(hào)') pro = ts.pro_api()#對(duì)接tushare接口這里注意,你要填入自己在tushare的個(gè)人主頁(yè)中的token號(hào)之后,才能對(duì)接接口
end=input('請(qǐng)輸入類似20180211的開始日期') start=input('請(qǐng)輸入類似20180217的結(jié)束日期') datestart = datetime.datetime.strptime(start, '%Y%m%d') dateend = datetime.datetime.strptime(end, '%Y%m%d')#獲得起始時(shí)間 d=''#定義一個(gè)空字符串end和start的命名反了是因?yàn)槭前硰木W(wǎng)上扒來(lái)的代碼,就懶得改了,湊合著用吧。
while datestart >= dateend:url_time = datestart.strftime('%Y%m%d')datestart -= datetime.timedelta(days=1)df = pro.cctv_news(date=url_time)b['A1']=url_timefor i in range(len(df.index)):b['A'+str(i+2)]=df.index[i]b['B'+str(i+2)]=df['title'][i]b['C'+str(i+2)]=df['content'][i]d = d + b['B' + str(i + 2)].value + b['C' + str(i + 2)].valuec.save(url_time+'新聞聯(lián)播.xlsx')print('已存入同級(jí)文件夾目錄下,命名為'+url_time+'新聞聯(lián)播.xlsx')if datestart >= dateend:time.sleep(31)#tushare限制訪問時(shí)間一分鐘2次讀取每一天的數(shù)據(jù),返回的類型是DataFrame.然后將其存入excel表格備用,同時(shí)將文稿都讀入字符串d中.
d=jieba.lcut(d) d=' '.join(d) g=imread(r'C:Users大帥哥Desktopstar.png')#大帥哥是俺的用戶名 e=wordcloud.WordCloud(width=1920,height=1920,background_color=(255,255,255),stopwords={},mask=g,font_path=r'C:WindowsFontsmsyh.ttc') e.generate(d) pic=e.to_image() e.to_file(end+'至'+start+'新聞聯(lián)播詞云.png') pic.show()用jieba進(jìn)行分詞,最后用五星制作為分詞就ok了.
圖與程序不符,但只有2020710-712的沒有敏感詞下面是完整代碼
import wordcloud import jieba import openpyxl from imageio import imread import datetime import tushare as ts import time c=openpyxl.Workbook()#創(chuàng)建一個(gè)工作簿 b=c.active#創(chuàng)建一個(gè)工作表 ts.set_token('token號(hào)') pro = ts.pro_api()#對(duì)接tushare接口 end=input('請(qǐng)輸入類似20180211的開始日期') start=input('請(qǐng)輸入類似20180217的結(jié)束日期') datestart = datetime.datetime.strptime(start, '%Y%m%d') dateend = datetime.datetime.strptime(end, '%Y%m%d')#獲得起始時(shí)間 d=''#定義一個(gè)空字符串 while datestart >= dateend:url_time = datestart.strftime('%Y%m%d')datestart -= datetime.timedelta(days=1)df = pro.cctv_news(date=url_time)b['A1']=url_timefor i in range(len(df.index)):b['A'+str(i+2)]=df.index[i]b['B'+str(i+2)]=df['title'][i]b['C'+str(i+2)]=df['content'][i]d = d + b['B' + str(i + 2)].value + b['C' + str(i + 2)].valuec.save(url_time+'新聞聯(lián)播.xlsx')print('已存入同級(jí)文件夾目錄下,命名為'+url_time+'新聞聯(lián)播.xlsx')if datestart >= dateend:time.sleep(31)#tushare限制訪問時(shí)間一分鐘2次 d=jieba.lcut(d) d=' '.join(d) g=imread(r'C:Users大帥哥Desktopstar.png')#大帥哥是俺的用戶名 e=wordcloud.WordCloud(width=1920,height=1920,background_color=(255,255,255),stopwords={},mask=e,font_path=r'C:WindowsFontsmsyh.ttc') e.generate(d) pic=e.to_image() e.to_file(end+'至'+start+'新聞聯(lián)播詞云.png') pic.show()總結(jié)
以上是生活随笔為你收集整理的tushare pro接口_利用tushare获取新闻联播文字稿并制作词云的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python multiprocessi
- 下一篇: 串行和并行的区别_入门参考:从Go中的协