python词频统计网页_【python 编程】网页中文过滤分词及词频统计

本文介绍如何使用Python进行网页爬取、正则表达式匹配、中文字符过滤、分词处理和词频统计。通过示例,讲解了全局变量、字符串处理、文件读写以及命令行操作等技巧,并给出了程序执行结果,展示了如何统计网页中的中文名词及其频率。
摘要由CSDN通过智能技术生成

从下面实例中可以学习到网页爬取,正则匹配,字符串过滤存储,文件读写等知识点,详细如下:

1、全局变量在函数中使用时需要加入global声明

2、获取网页内容存入文件时的编码为ascii进行正则匹配时需要decode为GB2312,当匹配到的中文写入文件时需要encode成GB2312写入文件。

3、中文字符匹配过滤正则表达式为ur'[\u4e00-\u9fa5]+',使用findall找到所有的中文字符存入分组

4、KEY,Value值可以使用dict存储,排序后可以使用list存储

5、字符串处理使用split分割,然后使用index截取字符串,判断哪些是名词和动词

6、命令行使用需要导入os,os.system(cmd)

7、下面程序执行结果如下,即找到网页中文名词及词频统计

doc_no word count

1 系统 5

1 账号 2

1 密码 2

1 分辨率 2

1 用户名 1

1 用户 1

1 软件 1

1 苹果 1

1 密码技术 1

1 宽度 1

1 火狐 1

# -*- coding: cp936 -*- import urllib2 import re import sys import os #os.system(cmd)os.system('ls') doc_no = 1 def start_fun():         reload(sys)         sys.setdefaultencoding('utf-8')         print sys.getdefaultencoding() def testfun():         str_test = "http://hao123.com\" class is >china"         searchstr= re.findall(r'http://(.*?)\"',str_test)         print searchstr[0] def split_word(webpage_chinese_file,webpage_chinese_word_file):         #调用命令行对中文文档进行分词处理         os.system('cd splitword && ictclas_demo_c.exe ../'+ webpage_chinese_file + ' ../'+webpage_chinese_word_file ) def doc_word_count_deal(webpage_chinese_word_file,webpage_all_word_count_docno):         global doc_no         word_dicts = dict()         #1、读取中文分词内容到缓存中         word_file = open(webpage_chinese_word_file,"r")         word_buf = word_file.read()         word_file.close()         #2、将分词以空格分隔并按/过滤提取分词中文内容和词性,不是名词或者长度小于2的不进行记录         word_sets =  word_buf.split(' ')         for i in word_sets:                 #print i                 if i == "" :                         continue                 j = i.index('/')                 #print 'j='+str(j)                 k = i[j+1:j+2]                 i = i[0:j]                                                   #print i                 #word_dicts[i]如果不存在KEY则会报KeyError错误                 if len(i) <= 2 or (k != None and k != 'n'):                         #print 'k='+k                         continue                 if word_dicts.get(i) == None :                         word_dicts[i] = 1                 else:                         word_dicts[i] = word_dicts[i] + 1         #sorted(word_dicts.viewvalues())         #list ->word_dicts = [ v for v in sorted(word_dicts.values())]         #3、将过滤后的中文分词按照出现次数进行排序,将排序好的数据存储到相应的文件中                                word_count = open(webpage_all_word_count_docno,'w')         word_dicts_list = sorted([(v, k) for k, v in word_dicts.items()], reverse=True)         for i in word_dicts_list:                 print i[1],i[0]                 word_count.write(str(doc_no)+" "+i[1]+" "+str(i[0])+"\n")         word_count.close()         doc_no = doc_no + 1                                  """         for i in word_dicts.viewkeys():                 print i,word_dicts[i]         """         def get_webpage_china(url_addr,webpage_src_file,webpage_chinese_file):         #reload( sys )         #sys.setdefaultencoding('utf-8')         print sys.getdefaultencoding()         #1、获取URL地址网页内容,默认为ascii,GB2312编码         url = url_addr         content = urllib2.urlopen(url).read()         #2、打开文件将网页内容写入文件                 file = open(webpage_src_file,'w')         file.write(content)         file.close         #str=f.read()         #fp.write(str.encode("utf-8"))                  #3、正则匹配获取网页中alert中所对应的内容         """         pattern1 = re.findall(r'alert\(\"(.*)\"\)',content)         for i in pattern1:                 print i                 print 'hello world!\n'         sub1 = re.sub(r'alert\(\"(.*)\"\)','Hello World!',content)         """         #chinaeseStr = re.match(ur".*[\u4e00-\u9fa5]+",content)         #a="

百度知道

"         #(?<=>)[^a-zA-Z0-9_]+(?=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值