(點選上方公眾號,可快速關註一起學Python)
來源:挖數 連結:
https://www.zhihu.com/question/28975391
對《還珠格格》進行詞頻統計
對《還珠格格》的詞頻統計生成詞雲標簽
將《2016年中國政府工作報告》變成詞雲是這樣的
然後是《小時代》
以小燕子照片為詞雲背景
對《射鵰英雄傳》進行詞頻統計並以郭靖劇照作為詞雲背景
有沒有滿滿的即視感?
一個Web端的電影資料庫互動
可以瞭解整個香港電影史,從早期合拍上海片,到胡金栓的武俠片,到李小龍時代,然後是成龍,接著周星馳
對職責要求的詞頻分析,提煉出必需技能
用爬蟲爬下上萬知乎女神照片
最後,展示一下Python程式碼:
詞頻統計和詞雲的程式碼
from wordcloud import WordCloud
import jieba
import PIL
import matplotlib.pyplot as plt
import numpy as np
def wordcloudplot(txt):
path = 'd:/jieba/msyh.ttf'
path = unicode(path, 'utf8').encode('gb18030')
alice_mask = np.array(PIL.Image.open('d:/jieba/she.jpg'))
wordcloud = WordCloud(font_path=path, background_color="white", margin=5, width=1800, height=800, mask=alice_mask, max_words=2000, max_font_size=60, random_state=42)
wordcloud = wordcloud.generate(txt)
wordcloud.to_file('d:/jieba/she2.jpg')
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
def main():
a = []
f = open(r'd:\jieba\book\she.txt', 'r').read()
words = list(jieba.cut(f))
for word in words:
if len(word) > 1:
a.append(word)
txt = r' '.join(a)
wordcloudplot(txt)
if __name__ == '__main__':
main()
爬知乎女神的程式碼
import requests
import urllib
import re
import random
from time import sleep
def main():
url = 'xxx'
essay-headers = {xxx}
i = 925
for x in xrange(1020, 2000, 20):
data = {'start': '1000',
'offset': str(x),
'_xsrf': 'a128464ef225a69348cef94c38f4e428'}
content = requests.post(url, essay-headers=essay-headers, data=data, timeout=10).text
imgs = re.findall(', content)
for img in imgs:
try:
img = img.replace('\\', '')
pic = img + '.jpg'
path = 'd:\\bs4\\zhihu\\jpg4\\' + str(i) + '.jpg'
urllib.urlretrieve(pic, path)
print ('下載了第' + str(i) + u'張圖片')
i += 1
sleep(random.uniform(0.5, 1))
except:
print ('抓漏1張')
pass
sleep(random.uniform(0.5, 1))
if __name__ == '__main__':
main()