【python】获取网页中中文内容并分词

 1 # -*- coding: utf-8 -*-
 2 
 3 import urllib2
 4 import re
 5 import time
 6 import jieba
 7 
 8 
 9 url="http://www.baidu.com"
10 html=urllib2.urlopen(url).read()
11 html=unicode(html,'utf-8')
12 word=re.findall(ur"[\u4e00-\u9fa5]+",html)
13 
14 s=""
15 for w in word:
16     s+=w
17     
18 seg_list=jieba.cut(s,cut_all=False)
19 fenci="/ ".join(seg_list)
20 print 'get web-->',s
21 print 'div result-》',fenci
22 time.sleep(10)

其中使用了 urllib2 re jieba三个模块 第一个模块用于获得网页内容,第二个模块用正则表达式提取中文字符 第三个模块用于分词

参考:

http://zhidao.baidu.com/link?url=4nU9JTj_GsObZExTum1jHRiwdDgEPnRl_oh7Msri3gfBxpH3LdUcaHCtR0wvWl0WCRCrcAlli62veGVl5pw-kK

http://www.cnblogs.com/mmix2009/p/3220427.html

【附】安装python模块将其下载后将对应的文件夹拷入python安装目录下的 Lib/site-packages/  下

点赞