|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +import json |
| 4 | +import re |
| 5 | +import time |
| 6 | +from http.cookies import SimpleCookie |
| 7 | + |
| 8 | +import jieba.analyse |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import requests |
| 11 | +from scipy.misc import imread |
| 12 | +from wordcloud import WordCloud |
| 13 | + |
| 14 | +from pymongo import MongoClient |
| 15 | + |
| 16 | + |
| 17 | +class Conn(object): |
| 18 | + client = MongoClient('localhost', 27017) |
| 19 | + db = client['weixin-comment'] |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def insert_many(cls, data): |
| 23 | + cls.db['comments'].insert_many(data) |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def query(cls): |
| 27 | + data = cls.db['comments'].find() |
| 28 | + return data |
| 29 | + |
| 30 | + |
| 31 | +conn = Conn() |
| 32 | + |
| 33 | +raw_cookie = """ |
| 34 | +gsScrollPos-5517=; tvfe_boss_uuid=9c139f72f8ae693f; pac_uid=1_253421576; pgv_pvi=5182785536; RK=0IMfVbYuWK; |
| 35 | +""" |
| 36 | + |
| 37 | +cookie = SimpleCookie(raw_cookie) |
| 38 | +requests_cookies = dict([(c, cookie[c].value) for c in cookie]) |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + # 普通留言, 精选留言总数 |
| 43 | + normal_count, selected_count = 141, 100 |
| 44 | + # 普通留言url |
| 45 | + normal_url = "https://mp.weixin.qq.com/misc/appmsgcomment?" \ |
| 46 | + "action=list_comment&" \ |
| 47 | + "mp_version=7&" \ |
| 48 | + "type=0&" \ |
| 49 | + "comment_id=2881104117&" \ |
| 50 | + "begin={begin}&" \ |
| 51 | + "count=10&" \ |
| 52 | + "token=1300595798&" \ |
| 53 | + "lang=zh_CN" |
| 54 | + |
| 55 | + # 精选留言url |
| 56 | + selected_url = "https://mp.weixin.qq.com/misc/appmsgcomment?action=list_comment&mp_version=7&type=1" \ |
| 57 | + "&begin={begin}&count=10&comment_id=2881104117&token=1300595798&lang=zh_CN" |
| 58 | + |
| 59 | + dd = dict([(normal_count, selected_url), (selected_count, normal_url)]) |
| 60 | + |
| 61 | + for k, v in dd.items(): |
| 62 | + crawler(k, v) |
| 63 | + |
| 64 | + |
| 65 | +def crawler(count, url): |
| 66 | + for i in range(0, count, 10): |
| 67 | + r = requests.get(url.format(begin=i), cookies=requests_cookies) |
| 68 | + match = re.search(r'"comment":(\[\{.*\}\])', r.text, re.S) |
| 69 | + if match: |
| 70 | + data = json.loads(match.group(1), encoding="utf-8") |
| 71 | + conn.insert_many(data) |
| 72 | + time.sleep(1) |
| 73 | + |
| 74 | + |
| 75 | +def display(): |
| 76 | + # 读取数据 |
| 77 | + |
| 78 | + data = conn.query() |
| 79 | + for c in data: |
| 80 | + yield c.get("content") |
| 81 | + |
| 82 | + |
| 83 | +def word_segment(texts): |
| 84 | + # 分词处理 |
| 85 | + jieba.analyse.set_stop_words("./stopwords.txt") |
| 86 | + for text in texts: |
| 87 | + tags = jieba.analyse.extract_tags(text, topK=20) |
| 88 | + yield " ".join(tags) |
| 89 | + |
| 90 | + |
| 91 | +def generate_img(texts): |
| 92 | + # 生成词云图片 |
| 93 | + data = " ".join(text for text in texts) |
| 94 | + |
| 95 | + mask_img = imread('./python-logo.png', flatten=True) |
| 96 | + wordcloud = WordCloud( |
| 97 | + font_path='/Library/Fonts//华文黑体.ttf', |
| 98 | + background_color='white', |
| 99 | + mask=mask_img |
| 100 | + ).generate(data) |
| 101 | + plt.imshow(wordcloud) |
| 102 | + plt.axis('off') |
| 103 | + plt.savefig('./wordcloud.jpg', dpi=600) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + main() |
| 108 | + # 生成词云方法 |
| 109 | + # generate_img(word_segment(display())) |
0 commit comments