-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
58 lines (43 loc) · 1.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from os import path
import re
import argparse
from wordcloud import WordCloud
import jieba
import requests
from lxml import html
def read_stopwords(file):
stopwords = set()
with open(file, 'rb') as f:
for line in f.readlines():
if not line.strip():
continue
stopwords.add(line.strip().decode('utf-8'))
return stopwords
stopwords = read_stopwords(path.join(path.dirname(__file__), 'stopwords.txt'))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--url', metavar='URL', required=True, help='input the url')
args = parser.parse_args()
url = args.url
output_file = path.join(path.dirname(__file__), 'wordcloud.png')
response = requests.get(url)
origin_text = response.text
origin_text = re.sub(r'<script.*?>.*?</script>', '', origin_text, flags=re.I|re.M|re.DOTALL)
origin_text = re.sub(r'<style.*?>.*?</style>', '', origin_text, flags=re.I|re.M|re.DOTALL)
doc = html.fromstring(origin_text)
text = doc.xpath('//body//text()')
text = [i.strip() for i in text if i.strip()]
text = ' '.join(text)
seg = jieba.cut(text)
seg = [i.strip() for i in seg if i.strip() and not i.strip().isdigit() and i.strip() not in stopwords]
seg = ' '.join(seg)
wordcloud = WordCloud(font_path='simhei.ttf', background_color='black', margin=5, width=1800, height=800)
wordcloud = wordcloud.generate(seg)
image = wordcloud.to_image()
with open(output_file, 'wb') as f:
image.save(f, format='png')
if __name__ == '__main__':
main()