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

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</b>"
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="<p class=\"w490\"> 百度知道</p> "
#(?<=>)[^a-zA-Z0-9_]+(?=<) 正则 ?<=只匹配最前面不进入分组,?=匹配最后不进入分组
#4、获取网页中文内容
chinaeseStr= re.findall(ur"[\u4e00-\u9fa5]+",content.decode('GB2312'))
#5、将中文内容写入文件
file_chinese = open(webpage_chinese_file,'w')
for i in chinaeseStr:
#print i
file_chinese.write(i.encode('GB2312'))
file_chinese.close()

def main():
url_addr = 'http://192.168.1.170:8000/jsoa/CheckUser.jspx'
webpage_src_file = 'webpage_src_file.txt'
webpage_chinese_file = 'webpage_chinese_file.txt'
webpage_chinese_word_file = 'webpage_chinese_word_file.txt'
webpage_all_word_count_docno = 'webpage_all_word_count_docno.txt'


get_webpage_china(url_addr,webpage_src_file,webpage_chinese_file)
split_word(webpage_chinese_file,webpage_chinese_word_file)
doc_word_count_deal(webpage_chinese_word_file,webpage_all_word_count_docno)


main()

更多相关文章

  1. linux notepadqq不支持中文输入的原因分析
  2. 《LINUX SHELL脚本攻略》(Sarath Lakshman中文版带书签) 和 英文
  3. Linux安装ElasticSearch-2.2.0-分词器插件(IK)
  4. pl/sql数据插入出现中文乱码问题
  5. ubuntu16.04 mysql5.7.20表中插入中文显示???的解决方法
  6. PL/SQL中复制中文再粘贴出现乱码问题的解决【转】
  7. java+mysql中文乱码问题
  8. 在CMD查看Mysql数据时出现中文乱码
  9. sqlserver2008r2查找非中文字母数字出现的第一个位置

随机推荐

  1. Linux cat 命令源码剖析
  2. Apache性能调节(摘自于Apache经典实例)
  3. Linux搭载ISO镜像为本地yum源
  4. Linux内存管理 (10)缺页中断处理
  5. linux shell脚本指令
  6. PowerPC平台 linux移植一
  7. Linux 软件安装到 /usr,/usr/local/ 还是
  8. 请问有谁知道linux上生成的共享库中,使用n
  9. linux kernal oom killer 学习
  10. linux获取网线插拔状态的实现