自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Python來看3天破10億的《我不是藥神》到底神在哪?

原創(chuàng)
開發(fā) 后端 開發(fā)工具
《我不是藥神》是由文牧野執(zhí)導(dǎo),徐崢、王傳君、周一圍、譚卓、章宇、楊新鳴等主演的喜劇電影,于 2018 年 7 月 6 日在中國上映。

【51CTO.com原創(chuàng)稿件】《我不是藥神》是由文牧野執(zhí)導(dǎo),徐崢、王傳君、周一圍、譚卓、章宇、楊新鳴等主演的喜劇電影,于 2018 年 7 月 6 日在中國上映。

影片在未上映前,大規(guī)模的點映積攢了相當(dāng)高的人氣和口碑, 截止 7 月 9 日凌晨:豆瓣評分:9.0 分,貓眼:9.7 分,淘票票:9.5 分,時光網(wǎng):8.8 分 。

為什么我說這三個網(wǎng)站呢,因為我們今天近 5000+ 條短評數(shù)據(jù)就來自于此,用專業(yè)的數(shù)據(jù)更有說服力。

綜合幾家的數(shù)據(jù):五星推薦如此之高,生活環(huán)境是真實的,情緒是真實的,困境也是真實的,甚至女主角是一個真實的上了年紀(jì)的美女,有真實的皺紋!真實才能帶來沉浸體驗。表面說的是藥,深層說的是命。

藥能治病,命卻不由自主,直面中國底層生命的苦難和尊嚴(yán),也沒有逃避對社會制度和商業(yè)法則的拷問,這是影片鍥入中國現(xiàn)實的關(guān)鍵,也是引發(fā)大眾共鳴的核心。

盛世危言,卻讓人能看到希望,這部影片極有可能成為 2018 年最具有爆炸性的話題。這也許就是未播先火,豆瓣 16 年后首部 9.0 高分電影的原因。

今天我們用 5000+ 條數(shù)據(jù)來分析一下,哪些地區(qū),什么樣的人,喜歡這部電影。

程勇只是個賣印度神油的小販,日子過的還湊合。老爹血管瘤急著做手術(shù),住院沒錢,妻子要帶兒子移民去國外發(fā)展,靠賣印度神油掙來的錢連水電費(fèi)都交不起,處處都需要錢。

[[235833]]

神秘男子呂受益找到程勇,讓他從印度幫忙代購一款藥物。呂受益患有血液癌癥,需要長期服用抗癌藥物進(jìn)行治療。

[[235834]]

正版藥「瑞士格列寧」非常昂貴,普通人家根本供應(yīng)不起,但在印度有一款仿制藥「印度格列寧」價格卻只有 1/20,但在中國是屬于禁藥,走私被抓,是需要負(fù)法律責(zé)任的。

在巨大利益的驅(qū)使下,思慧,神父,黃毛先后出場,賣藥五人組團(tuán)建成功,他成為一名“藥販子”。

[[235835]]

對于病友來說,他們擁有了活下去的機(jī)會,紛紛給程勇送錦旗,自此稱其為“藥神”。

代購的藥出現(xiàn)問題,假藥販子張長林的出現(xiàn)威脅程勇,怕被抓,賣藥組正式散伙。

程勇開了工廠,呂受益死,張長林跑路,讓程勇完成第一次蛻變,許多病人無藥可吃,程勇再次去印度并重新團(tuán)建賣藥。

警方嚴(yán)打假藥販子,張長林被抓。警方發(fā)現(xiàn)程勇窩點,黃毛為了掩護(hù)程勇而死,讓他完成第二次蛻變。

以賠本價繼續(xù)代購印度藥,送兒子移民,晚上賣藥被警察抓。三年后出獄,外面已是改天換地。

《我不是藥神》的現(xiàn)實意義大于電影本身,許多人評論這部電影都有些揚(yáng)眉吐氣的感覺,大家都在做一個中國電影終于敢說真話的夢。

截止 7 月 9 日凌晨,累積票房超過 13 個億,占當(dāng)天票房近 84%。

是哪些地區(qū)貢獻(xiàn)的票房更多一些,通過數(shù)據(jù)分析發(fā)現(xiàn):

如這張圖片動態(tài)展示的情況,你會發(fā)現(xiàn)貢獻(xiàn)最多的還是:北京、上海、廣州,二線城市同樣成為票房的貢獻(xiàn)者。

[[235836]]

從畫像來看,更趨于中年,油膩的中年,人人都怕老病死,人人都怕上下為難,人人都有為謀生計不得不做的事情,人人亦都向往真與善……是這些時刻集中起來讓煽情的《藥神》不那么脫離現(xiàn)實。

從數(shù)據(jù)上來看,好看,現(xiàn)實,好片,感人,淚點,作品很棒。

“領(lǐng)導(dǎo),我求求你,別再查「假藥」了行么。這藥假不假,我們這些吃的人還不知道么?”

”我吃了三年正版藥,房子吃沒了,家也吃垮了。現(xiàn)在好不容易有了便宜藥,可你們非說這是「假藥」。不吃藥,我們就只能等死。”

[[235837]]

《我不是藥神》戳中的是每個人的痛點,誰能保證這一輩子自己和家人不生病呢?

一旦遇上大病,動輒上萬的高昂醫(yī)藥費(fèi)讓普通人家根本無力承擔(dān)。一人生病,全家拖垮,真不是危言聳聽。

下面我們回歸技術(shù),分享一下我們?nèi)绾潍@取的數(shù)據(jù):

首先是豆瓣,豆瓣自從去年 10 月份已經(jīng)全面禁止爬取數(shù)據(jù),僅僅放出 500 條數(shù)據(jù),豆瓣封 IP,白天一分鐘可以訪問 40 次,晚上一分鐘可以訪問 60 次,超過限制次數(shù)就會封 IP。 

  1. import urllib 
  2. import requests 
  3. from urllibimport request 
  4. import time 
  5. header = {'User-Agent''Mozilla/5.0 (Windows NT 10.0; Win32; x32; rv:54.0) Gecko/20100101 Firefox/54.0'
  6. 'Connection''keep-alive'
  7. cookies = 'v=3; iuuid=1A6E888B4A4B29B16FBA1299108DBE9CDCB327A9713C232B36E4DB4FF222CF03; webp=true; ci=1%2C%E5%8C%97%E4%BA%AC; __guid=26581345.3954606544145667000.1530879049181.8303; _lxsdk_cuid=1646f808301c8-0a4e19f5421593-5d4e211f-100200-1646f808302c8; _lxsdk=1A6E888B4A4B29B16FBA1299108DBE9CDCB327A9713C232B36E4DB4FF222CF03; monitor_count=1; _lxsdk_s=16472ee89ec-de2-f91-ed0%7C%7C5; __mta=189118996.1530879050545.1530936763555.1530937843742.18' 
  8. def html_prase(url): 
  9. r = requests.get(url).content 
  10. return r 
  11. cookie = {} 
  12. for line in cookies.split(';'): 
  13. name, value = cookies.strip().split('=', 1) 
  14.     cookie[name] = value 
  15. def html_prase(url): 
  16. r = requests.get(url).content 
  17. return r 
  18. for iin range(1, 100): 
  19. print('正在打印第%s頁' % i) 
  20. try: 
  21. url= 'http://m.maoyan.com/mmdb/comments/movie/1200486.json?_v_=yes&offset=%s&' % ( 
  22. i* 15)  
  23. print(url) 
  24.         proxy = html_prase('http://172.17.0.29:5010/get/')..decode('utf-8')  # 代理是自建代理池,有需要使用代理的可以聯(lián)系我,知乎ID:布道 
  25. html = requests.get(url=url, cookies=cookie, headers=header, 
  26. proxies={"http""http://{}".format(proxy)}).content 
  27.         data = json.loads(html.decode('utf-8'))['cmts'
  28. for item in data: 
  29. comment = item['content'
  30.             date = item['time'].split(' ')[0] 
  31.             rate = item['score'
  32.             city = item['cityName'
  33. img= item['avatarurl'
  34. print(date, rate, comment, city, ) 
  35. with open('maoyan_08.txt''a', encoding='utf-8'as f: 
  36. f.write(date + ',' + str(rate) + ',' + comment + ',' + comment + ',' + city + '\n'
  37. if img: 
  38. f = open('C:\\Users\My\Desktop\yaoshen\img\\' + img.split('/')[-1], 'wb') 
  39. f.write((urllib.request.urlopen(img)).read()) 
  40. except
  41. continue 
  42. time.sleep(5 + float(random.randint(1, 100)) / 20) 

另外一種方式:(Anyproxy+JS+Python+Monkeyrunner),可以爬取 Web 靜態(tài)網(wǎng)站、App 應(yīng)用、JS 渲染數(shù)據(jù)的動態(tài)網(wǎng)站的數(shù)據(jù)都可以進(jìn)行爬取。

安裝使用,請查閱官方 Github:https://github.com/alibaba/anyproxy

JS 代碼:

  1. var logMap = {} 
  2. var fs = require('fs'); 
  3. var iconv = require('iconv-lite'); 
  4. var logger = fs.createWriteStream('./urlLog.log', { 
  5.     flags: 'a' // 'a' means appending (old data will be preserved) 
  6. }) 
  7. function logPageFile(url) { 
  8.     if (!logMap[url]) { 
  9.         logMap[url] = true
  10.         logger.write(url + '\r\n'); 
  11.     } 
  12. function postData(post_data, path, cb) { 
  13.     // // Build the post string from an object 
  14.     // var post_data = JSON.stringify({ 
  15.     //     'data': data 
  16.     // }); 
  17.  
  18.     // An object of options to indicate where to post to 
  19.     var post_options = { 
  20.         host: '127.0.0.1'
  21.         port: '9999'
  22.         path: '/' + path, 
  23.         method: 'POST'
  24.         headers: { 
  25.             'Content-Type''application/json'
  26.             'Content-Length': Buffer.byteLength(post_data) 
  27.         } 
  28.     }; 
  29.  
  30.     var http = require('http'); 
  31.     // Set up the request 
  32.     var post_req = http.request(post_options, function (res) { 
  33.         res.setEncoding('utf8'); 
  34.         res.on('data', cb); 
  35.     }); 
  36.  
  37.     logger.write('request post data 1\r\n'
  38.  
  39.     // post the data 
  40.     post_req.write(post_data); 
  41.  
  42.     logger.write('request post data 2\r\n'
  43.     post_req.end(); 
  44.  
  45. module.exports = { 
  46.     summary: 'a rule to modify response'
  47.     * beforeSendResponse(requestDetail, responseDetail) { 
  48.  
  49.       if (/movie\/1200486/i.test(requestDetail.url)) { 
  50.           logger.write('matched: ' + requestDetail.url + '\r\n'); 
  51.           if (responseDetail.response.toString() !== "") { 
  52.               logger.write(responseDetail.response.body.toString()); 
  53.               var post_data = JSON.stringify({ 
  54.                   'url': requestDetail.url, 
  55.                   'body': responseDetail.response.body.toString() 
  56.               }); 
  57.               logger.write("post comment to server -- ext"); 
  58.               postData(post_data, 'douban_comment'function (chunk) { 
  59.               }); 
  60.          } 
  61.       } 
  62.     }, 
  63. }; 

使用 AnyProxy 加載 JS 代碼:anyproxy -i --rule wxrule.js

  1. #!/usr/bin/env python3 
  2.  
  3. import asyncio 
  4. import re 
  5. import textwrap 
  6. import threading 
  7. import time 
  8.  
  9. import os 
  10. import pymysql 
  11. from mysqlmgrimport MysqlMgr 
  12. from mongomgrimport MongoManager 
  13. from subprocess import call 
  14. import requests 
  15. from lxmlimport etree 
  16. from lxmlimport html 
  17. from aiohttp.webimport Application, Response, StreamResponse, run_app 
  18. import json 
  19. STATE_RUNNING = 1 
  20. STATE_IN_TRANSACTION = 2 
  21. running_state= 0 
  22. run_swipe= True 
  23. last_history_time= time.clock() 
  24. # A thread to save data to database in background 
  25. def insert_to_database(biz, msglist): 
  26. try: 
  27. for msg in msglist: 
  28. print(biz) 
  29. print(msg['comm_msg_info']['id']) 
  30. mongo_mgr.enqueue_data(msg['comm_msg_info']['id'], biz, msg ) 
  31. except Exception as e: 
  32. print(e) 
  33. def save_data(biz, msglist_str): 
  34. save_thread= threading.Thread(target=insert_to_database, args=(biz, msglist_str,)) 
  35. save_thread.setDaemon(True
  36. save_thread.start() 
  37.  
  38. def swipe_for_next_page(): 
  39. while run_swipe: 
  40. time.sleep(5) 
  41. if time.clock() - last_history_time>120: 
  42. if running_state== STATE_RUNNING: 
  43. reenter() 
  44. continue 
  45. call(["adb""shell""input""swipe""400""1000""400""200"]) 
  46. def reenter(): 
  47. global running_state 
  48. running_state= STATE_IN_TRANSACTION 
  49. # 模擬側(cè)滑實現(xiàn)返回上一頁 
  50. call(["adb""shell""input""swipe""0""400""400""400"]) 
  51. time.sleep(2) 
  52. # 點擊"進(jìn)入歷史消息",每個手機(jī)的位置不一樣,需要單獨設(shè)置 X  和 Y 
  53. call(["adb""shell""input""tap""200""1200"]) 
  54. time.sleep(2) 
  55. header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0','Connection':'keep-alive'
  56. def html_prase(url): 
  57. r = requests.get(url,header).content 
  58. return html.fromstring(r) 
  59.  
  60. async def report_url(request): 
  61. resp = StreamResponse() 
  62.     data = await request.json() 
  63. url= data['url'
  64. # print("url reported: " + url) 
  65. biz = re.findall('__biz=(.*?)\&', url) 
  66. if len(biz) == 0: 
  67. await resp.prepare(request) 
  68. return resp 
  69.     biz = biz[0] 
  70. print('----------------\r\n'+ biz + '\r\n----------------\r\n'
  71. mysql_mgr.enqueue_biz(biz, ''
  72. bizs.add(biz) 
  73.     biz = biz.encode('utf8'
  74. resp.content_type= 'text/plain' 
  75. await resp.prepare(request) 
  76. resp.write(biz) 
  77. await resp.write_eof() 
  78. return resp 
  79. async def intro(request): 
  80. txt = textwrap.dedent("""\ 
  81.         Type {url}/hello/John  {url}/simple or {url}/change_body 
  82.         in browser url bar 
  83.     """).format(url='127.0.0.1:8080'
  84.     binary = txt.encode('utf8'
  85.     resp = StreamResponse() 
  86. resp.content_length= len(binary
  87. resp.content_type= 'text/plain' 
  88. await resp.prepare(request) 
  89. resp.write(binary
  90. return resp 
  91. async def simple(request): 
  92. return Response(text="Simple answer"
  93. async def change_body(request): 
  94. resp = Response() 
  95. resp.body= b"Body changed" 
  96. resp.content_type= 'text/plain' 
  97. return resp 
  98. # coding=utf-8 
  99. async def app_douban_comment(request): 
  100. resp = StreamResponse() 
  101.     data = await request.json() 
  102. global running_state 
  103. global last_history_time 
  104. msg_data= json.loads(data['body'])['data']['cts'
  105. for item in msg_data: 
  106. comment = item['ce'].strip().replace('\n',''
  107.         rate = item['cr'
  108. print(comment, rate) 
  109. with open('date_rate_comment_sg.txt''a', encoding='utf-8'as f: 
  110. f.write('2018-07-06' + ',' + str(rate) + ',' + comment + '\n'
  111. last_history_time= time.clock() 
  112. resp.content_type= 'text/plain' 
  113. await resp.prepare(request) 
  114. await resp.write_eof() 
  115. return resp 
  116. last_history_time= time.clock() 
  117. resp.content_type= 'text/plain' 
  118. await resp.prepare(request) 
  119. await resp.write_eof() 
  120. return resp 
  121. async def init(loop): 
  122. app = Application() 
  123. app.router.add_get('/', intro) 
  124. app.router.add_post('/url', report_url) 
  125. app.router.add_post('/douban_comment', app_douban_comment) 
  126. return app 
  127. def start_swipe_thread(): 
  128. try: 
  129. t = threading.Thread( 
  130. target=swipe_for_next_page, name='swipe'
  131. set daemon so main thread can exit when receives ctrl-c 
  132. t.setDaemon(True
  133. t.start() 
  134. except Exception: 
  135. print("Error: unable to start thread"
  136. loop = asyncio.get_event_loop() 
  137. app = loop.run_until_complete(init(loop)) 
  138. run_app(app, host='127.0.0.1', port=9999) 

這是示例代碼,實際使用過程,需要進(jìn)行微調(diào)。獲取貓眼數(shù)據(jù),最難是難在找貓眼 App 的數(shù)據(jù)接口。

我費(fèi)了很大力氣才找到:http://m.maoyan.com/mmdb/comments/movie/1200486.json?_v_=yes&offset=15'

接口怎么使用,直接看代碼,獲取淘票票的數(shù)據(jù)需要你自己去嘗試找一下。

  1. import json 
  2. import random 
  3. import urllib 
  4. import requests 
  5. from urllibimport request 
  6. import time 
  7. header = {'User-Agent''Mozilla/5.0 (Windows NT 10.0; Win32; x32; rv:54.0) Gecko/20100101 Firefox/54.0'
  8. 'Connection''keep-alive'
  9. cookies ='v=3; iuuid=1A6E888B4A4B29B16FBA1299108DBE9CDCB327A9713C232B36E4DB4FF222CF03; webp=true; ci=1%2C%E5%8C%97%E4%BA%AC; __guid=26581345.3954606544145667000.1530879049181.8303; _lxsdk_cuid=1646f808301c8-0a4e19f5421593-5d4e211f-100200-1646f808302c8; _lxsdk=1A6E888B4A4B29B16FBA1299108DBE9CDCB327A9713C232B36E4DB4FF222CF03; monitor_count=1; _lxsdk_s=16472ee89ec-de2-f91-ed0%7C%7C5; __mta=189118996.1530879050545.1530936763555.1530937843742.18' 
  10. cookie = {} 
  11. for line in cookies.split(';'): 
  12. name, value = cookies.strip().split('=', 1) 
  13.     cookie[name] = value 
  14. def html_prase(url): 
  15. r = requests.get(url).content 
  16. return r 
  17. for iin range(1, 100): 
  18. print('正在打印第%s頁' % i) 
  19. try: 
  20. url= 'http://m.maoyan.com/mmdb/comments/movie/1200486.json?_v_=yes&offset=%s&' %(i*15) +'startTime=2018-07-01%2012%3A30%3A42' 
  21. print(url) 
  22.         html = requests.get(url=url, cookies=cookie, headers=header).content 
  23.         data = json.loads(html.decode('utf-8'))['cmts'
  24. for item in data: 
  25. comment = item['content'
  26.             date = item['time'].split(' ')[0] 
  27.             rate = item['score'
  28.             city = item['cityName'
  29. img= item['avatarurl'
  30. print(date, rate, comment, city, ) 
  31. with open('maoyan_08.txt''a', encoding='utf-8'as f: 
  32. f.write(date + ',' + str(rate) + ',' + comment +',' + comment + ','+ city +'\n'
  33. if img: 
  34. f = open('C:\\Users\My\Desktop\yaoshen\img\\' + img.split('/')[-1], 'wb') 
  35. f.write((urllib.request.urlopen(img)).read()) 
  36. except
  37. break 
  38. time.sleep(5 + float(random.randint(1, 100)) / 20) 

動態(tài)地圖展示代碼:

  1. from pyechartsimport Style 
  2. from pyechartsimport Geo 
  3. city =[] 
  4. with open('maoyan.txt', mode='r', encoding='utf-8'as f: 
  5. rows = f.readlines() 
  6. for row in rows
  7. if len(row.split(',')) == 5: 
  8. city.append(row.split(',')[4].replace('\n','')) 
  9. def all_list(arr): 
  10. result = {} 
  11. for iin set(arr): 
  12. result[i] = arr.count(i) 
  13. return result 
  14. data = [] 
  15. for item in all_list(city): 
  16. data.append((item,all_list(city)[item])) 
  17. style = Style( 
  18. title_color="#fff"
  19. title_pos="center"
  20. width=1200, 
  21. height=600, 
  22. background_color='#404a59' 
  23. geo = Geo( "《我不是藥神》評論人群地理位置","數(shù)據(jù)來源:知乎ID:布道", **style.init_style) 
  24. attr, value = geo.cast(data) 
  25. geo.add("", attr, value, visual_range=[0, 100], 
  26. visual_text_color="#fff", is_legend_show=False
  27. symbol_size=20, is_visualmap=True
  28. tooltip_formatter=''
  29. label_emphasis_textsize=15, 
  30. label_emphasis_pos='right'
  31. geo.render() 

每天爬取數(shù)據(jù)量代碼:

  1. from pyechartsimport EffectScatter 
  2. from pyechartsimport Style 
  3.  
  4. style= Style( 
  5. title_color="#191970"
  6. title_pos="left"
  7. width=900, 
  8. height=450, 
  9. background_color='#F8F8FF' 
  10. es = EffectScatter("《我不是藥神》短評數(shù)據(jù)情況","數(shù)據(jù)來源:知乎ID:布道", **style.init_style) 
  11. es.add("", [1], [270], symbol_size=20, effect_scale=4, 
  12. effect_period=5, symbol="pin"
  13. es.add("", [2], [606], symbol_size=20, effect_scale=4, 
  14. effect_period=5, symbol="pin"
  15. es.add("", [3], [542], symbol_size=20, effect_scale=4, 
  16. effect_period=5, symbol="pin"
  17. es.add("", [4], [550], symbol_size=20, effect_scale=4, 
  18. effect_period=5, symbol="pin"
  19. es.add("", [5], [656], ssymbol_size=20, effect_scale=4, 
  20. effect_period=5, symbol="pin"
  21. es.add("", [6], [850], ssymbol_size=20, effect_scale=4, 
  22. effect_period=5, symbol="pin"
  23. es.add("", [7], [993], symbol_size=20, effect_scale=4, 
  24. effect_period=5, symbol="pin"
  25. es.add("", [8], [903], symbol_size=20, effect_scale=4, 
  26. effect_period=5, symbol="pin"
  27.  
  28.  
  29. es.render() 

五星推薦河流圖代碼:

  1. from pyechartsimport Style 
  2. from pyechartsimport ThemeRiver 
  3.  
  4. data = [ 
  5.     ['2018/07/08', 802, '五星'], ['2018/07/08', 28, '四星'], ['2018/07/08', 9, '三星'], ['2018/07/08',8, '二星'], 
  6.     ['2018/07/08', 4, '一星'], 
  7.  
  8.     ['2018/07/07',802, '五星'], ['2018/07/07',166, '四星'], ['2018/07/07',17, '三星'],['2018/07/07',0, '二星'],['2018/07/07',8, '一星'], 
  9.     ['2018/07/06', 667, '五星'], ['2018/07/06', 156, '四星'], ['2018/07/06', 13, '三星'], ['2018/07/06', 10, '二星'],['2018/07/06', 4, '一星'], 
  10.     ['2018/07/05', 567, '五星'], ['2018/07/05', 76, '四星'], ['2018/07/05', 13, '三星'], ['2018/07/05', 0, '二星'],['2018/07/05', 0, '一星'], 
  11.     ['2018/07/04', 467, '五星'], ['2018/07/04', 67, '四星'], ['2018/07/04', 16, '三星'], ['2018/07/04', 0, '二星'],['2018/07/04', 0, '一星'], 
  12.     ['2018/07/03', 478, '五星'], ['2018/07/03', 56, '四星'], ['2018/07/03', 8, '三星'], ['2018/07/03', 0, '二星'],['2018/07/03', 0, '一星'], 
  13.     ['2018/07/02', 531, '五星'], ['2018/07/02', 67, '四星'], ['2018/07/02', 8, '三星'], ['2018/07/02', 0, '二星'],['2018/07/02', 0, '一星'], 
  14.     ['2018/07/01', 213, '五星'], ['2018/07/01', 45, '四星'], ['2018/07/01', 5, '三星'], ['2018/07/01', 1, '二星'], 
  15.     ['2018/07/01', 1, '一星'], 
  16.  
  17. style = Style( 
  18. title_color="#191970"
  19. title_pos="left"
  20. width=1200, 
  21. height=600, 
  22. background_color='#F8F8FF' 
  23. tr = ThemeRiver("《我不是藥神》星級推薦","數(shù)據(jù)來源:知乎ID:布道", **style.init_style) 
  24. tr.add(['五星''四星''三星''二星''一星',], data, is_label_show=True
  25. tr.render() 

詞云圖:

  1. import pickle 
  2. from osimport path 
  3. import jieba 
  4. import matplotlib.pyplotas plt 
  5. from wordcloudimport WordCloud, STOPWORDS, ImageColorGenerator 
  6. def make_worldcloud(file_path): 
  7. text_from_file_with_apath= open(file_path,'r',encoding='UTF-8').read() 
  8. wordlist_after_jieba= jieba.cut(text_from_file_with_apath, cut_all=False
  9. wl_space_split= " ".join(wordlist_after_jieba) 
  10. print(wl_space_split) 
  11. backgroud_Image= plt.imread('./1.jpg'
  12. print('加載圖片成功!'
  13. '''設(shè)置詞云樣式''' 
  14. stopwords= STOPWORDS.copy() 
  15. stopwords.add("哈哈"
  16. stopwords.add("電影"
  17. stopwords.add("真的"
  18. stopwords.add("就是"
  19. stopwords.add("真是"
  20. stopwords.add("中國"
  21. stopwords.add("沒有"
  22. stopwords.add("可以"
  23. stopwords.add("一部"
  24. stopwords.add("還是"
  25. stopwords.add("最后"
  26. stopwords.add("一個")  #可以加多個屏蔽詞#可以加多個屏蔽詞 
  27. wc= WordCloud( 
  28. width=1024, 
  29. height=768, 
  30. background_color='white',# 設(shè)置背景顏色 
  31. mask=backgroud_Image,# 設(shè)置背景圖片 
  32. font_path='E:\simsun.ttf',  # 設(shè)置中文字體,若是有中文的話,這句代碼必須添加,不然會出現(xiàn)方框,不出現(xiàn)漢字 
  33. max_words=600, # 設(shè)置最大現(xiàn)實的字?jǐn)?shù) 
  34. stopwords=stopwords,# 設(shè)置停用詞 
  35. max_font_size=400,# 設(shè)置字體最大值 
  36. random_state=50,# 設(shè)置有多少種隨機(jī)生成狀態(tài),即有多少種配色方案 
  37. wc.generate_from_text(wl_space_split)#開始加載文本 
  38. img_colors= ImageColorGenerator(backgroud_Image) 
  39. wc.recolor(color_func=img_colors)#字體顏色為背景圖片的顏色 
  40. plt.imshow(wc)# 顯示詞云圖 
  41. plt.axis('off')# 是否顯示x軸、y軸下標(biāo) 
  42. plt.show()#顯示 
  43.     # 獲得模塊所在的路徑的 
  44. d = path.dirname(__file__) 
  45. # os.path.join():  將多個路徑組合后返回 
  46. wc.to_file(path.join(d, "h11.jpg")) 
  47. print('生成詞云成功!'
  48.  
  49. make_worldcloud('cloud.txt'

圖像畫像代碼:

  1. import os 
  2. from math import sqrt 
  3. from PIL import Image 
  4. #path是存放好友頭像圖的文件夾的路徑 
  5. path = 'C:\\Users\My\Desktop\yaoshen\img\\' 
  6. pathList= [] 
  7. for item in os.listdir(path): 
  8. imgPath= os.path.join(path,item) 
  9. pathList.append(imgPath) 
  10. total = len(pathList)#total是好友頭像圖片總數(shù) 
  11. line = int(sqrt(total))#line是拼接圖片的行數(shù)(即每一行包含的圖片數(shù)量) 
  12. NewImage= Image.new('RGB', (128*line,128*line)) 
  13. x = y = 0 
  14. for item in pathList: 
  15. try: 
  16. img= Image.open(item) 
  17. img= img.resize((128,128),Image.ANTIALIAS) 
  18. NewImage.paste(img, (x * 128 , y * 128)) 
  19.         x += 1 
  20. except IOError: 
  21. print("第%d行,%d列文件讀取失??!IOError:%s" % (y,x,item)) 
  22.         x -= 1 
  23. if x == line: 
  24. x = 0 
  25. y += 1 
  26. if (x+line*y) == line*line: 
  27. break 
  28. NewImage.save(path+"final.jpg"

作者:劉曉明

簡介:互聯(lián)網(wǎng)公司運(yùn)維技術(shù)負(fù)責(zé)人,擁有 10 年的互聯(lián)網(wǎng)開發(fā)和運(yùn)維經(jīng)驗。一直致力于運(yùn)維工具的開發(fā)和運(yùn)維專家服務(wù)的推進(jìn),賦能開發(fā),提高效能。最后給自己代個鹽,歡迎大家有空時翻下我牌子(知乎號:布道,微信:AiDevOps),看看“開發(fā)運(yùn)維”專欄的文章和公眾號的文章,希望多些關(guān)注和點贊是給作者最好的鼓勵 !

【51CTO原創(chuàng)稿件,合作站點轉(zhuǎn)載請注明原文作者和出處為51CTO.com】

責(zé)任編輯:武曉燕 來源: 51CTO技術(shù)棧
相關(guān)推薦

2018-07-16 15:15:40

架構(gòu)技術(shù)棧微信半月刊

2018-07-16 16:39:00

數(shù)據(jù)

2020-04-17 08:14:34

盤神Pandownload軟件

2020-08-31 14:55:48

代碼開發(fā)AI

2019-08-06 14:15:23

Python數(shù)據(jù)分析哪吒

2020-03-04 13:55:44

程序員技能開發(fā)者

2019-01-04 16:13:01

新華三

2018-07-13 15:36:52

2020-04-09 09:52:42

Python數(shù)據(jù)技術(shù)

2012-10-30 15:53:29

測試測試驅(qū)動開發(fā)驅(qū)動開發(fā)

2018-07-12 13:10:01

新華三

2016-01-26 14:25:32

EasyStackOpenStack云計算

2017-12-18 17:03:56

Python程序員開發(fā)

2021-07-05 08:12:53

Nginx架構(gòu)多進(jìn)程

2022-01-04 09:24:32

Python Excel 表格

2019-12-20 12:38:28

Java技術(shù)工具

2020-05-20 12:44:53

編程軟件開發(fā)JavaScript

2018-07-25 13:47:51

彭于晏邪不壓正Python

2013-03-01 10:15:43

敏捷開發(fā)敏捷

2021-04-22 22:29:40

Python開發(fā)算法
點贊
收藏

51CTO技術(shù)棧公眾號