统计2016政府工作报告中的高频词

# -*- coding: utf-8 -*-
'''
  统计2016政府工作报告中的高频词
'''

import jieba
import requests
from bs4 import BeautifulSoup

'''
    从网上抓取政府工作报告的全文 extract_text(url)
'''
def extract_text(url):
    #page_source = requests.get(url).text   出现乱码  为了不乱可以设置编码  r.encoding='utf-8'
    page_source = requests.get(url).content   #以字节的方式访问请求响应体   会出现乱码
    bs_source = BeautifulSoup(page_source)
    report_text = bs_source.find_all('p')  #查找文档中所有的<p>标签  返回一个列表
    text=''
    for p in report_text:
        text += p.get_text()    #得到<p>标签之间的内容
        text += '\n'
    return text

'''
    利用jieba分词,并计算词频 word_frequency(text)
'''
def word_frequency(text):
    from collections import Counter

    #len(word)>=2 为了去除标点符号和当个停用词   words为一个list
    words = [word for word in jieba.cut(text, cut_all=True) if len(word)>=2]
    c = Counter(words)    #返回的c为一个字典  key是各个word   value是出现的次数

    for word_freq in c.most_common(10):   # most_common(n),列出最常出现的前n        word, freq = word_freq
        print word, freq


url_2016 = 'http://www.gov.cn/guowuyuan/2016-03/05/content_5049372.htm'
text_2016 = extract_text(url_2016)
word_frequency(text_2016)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值