#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("gbk")
from re import findall
import requests
import re
from urllib import urlopen
from bs4 import BeautifulSoup
def modifyip(tfile,sstr,rstr):
try:
lines=open(tfile,'r').readlines()
flen=len(lines)-1
for i in range(flen):
if sstr in lines[i]:
lines[i]=lines[i].replace(sstr,rstr)
open(tfile,'w').writelines(lines)
except Exception,e:
print e
url = 'http://mp.weixin.qq.com/s/l9J5CLSR2Oo83QwQx119vw'
content = urlopen(url).read()
f1 = file('p.htm','wb')
f1.write(content)
f1.close()
#f2=file('pic.txt','wb')
import os
import math
altxt = open('p.htm').read()
pattern = '<script(.+?)</script>'
#out = re.sub(pattern,' ', altxt)
listpp=re.split(r'<script|</script>|<style>|</style>',altxt)
ff=open('p1.htm','w+')
ff.close
f1 = file('p1.htm','a+')
for i in listpp:
print listpp.index(i)
if (listpp.index(i)+2)%2 == 0:
print i
f1.write(i)
f1.close()
#这上面是清除script
#目前对htm分词已经成功,剩下的就是在HTM中置入图片
#首先要把图片插入,这个用
#首先分词会把特殊的字符处理掉
#需要在分词之后再添加
#怎么处理分词之后的文本,怎样拆分分词之后的文本
#在找到图片网址之后,该怎样处理呢?
#增加什么特殊的字符,可以便于后期的处理呢
#这里只是修改p.htm,如果能记下文件名,后期就便于处理了。根据图片的文件名,找到相应位置,然后把文件拆分成不同的字符串,然后再合并就可以了
#这个思路不错,核心在于拆分文件。
# 对于t,写入P3.TXT,
#在这之后,就已经清除掉了图片网址,能不能忽略网址呢?
#在后面才进行图片的下载和处理,这之前还没有处理。 可以把图片处理放到前面吗?
#这下面已经用到了分割字符,只需要把这个分割字符,按照图片网址分割开,分别保存即可。
#仔细弄清楚它是怎么分割的。
#打开文件,
#pattern = '<script(.+?)</script>' 这句似乎没用
#listpp=re.split(r'<|>',altxt) 分割后,生成一个listapp
#
ff=open('p2.htm','w+')
ff.close
altxt = open('p1.htm').read()
#pattern = '<script(.+?)</script>'
#out = re.sub(pattern,' ', altxt)
listpp=re.split(r'<|>',altxt)
f2 = file('px.txt','a+')
for i in listpp:
#print listpp.index(i)
if (listpp.index(i)+2)%2 == 0:
#print i
f2.write(i+chr(10))
f1.close()
f1 = file('p2.txt','a+')
for i in listpp:
#print listpp.index(i)
if (listpp.index(i)+2)%2 == 0:
#print i
f1.write(i+chr(10))
if not i.find('data-src="')==-1 :#如果在i中包含img就保留,这样就可以包含图片了, 注意看一下,这种思路是对的。 只需要继续试下去即可。看一下原始格式。
jx=i.find('data-src="') #位置
sx=i[jx+10:] #截取后半段
jx1=sx.find('"')
wi=i[jx-1:jx+10+jx1+1]
f1.write(wi+chr(10))
#垃圾是从这个地方开始出现的,不要写i,而要裁剪。 只留下最重要的部分。怎么对i进行裁剪呢?首先它是一个字符串,从data-src开始裁剪,到第一个双引号的位置直到结束
#
f1.close()
#这个时候的P3是纯净的吗?,还是包含IMG的大部分垃圾
#为什么会写好几遍呢?
#这个地方生成的P2.TXT, 是巧妙地, 但是分词后会出问题。所以分词之前,就要妥善处理
#
"""
for i in range(1):
with open('p2.htm','r+') as f:
t = f.read()
t = t.replace('/head', '')
t = t.replace('head', '')
t = t.replace('/body', '')
t = t.replace('body', '')
t = t.replace('/html', '')
t = t.replace('html', '')
#t = t.replace(' ', '')
#读写偏移位置移到最开始处
f.seek(0, 0)
f.write(t)
"""
def delblankline(infile,outfile):
infopen = open(infile,'r')
outfopen = open(outfile,'w')
lines = infopen.readlines()
for line in lines:
if line.split():
outfopen.writelines(line)
else:
outfopen.writelines("")
infopen.close()
outfopen.close()
ff=open('p3.txt','w+')
ff.close
delblankline("p2.txt","p3.txt")
#这一部分是剔除空行
#先分割再插入,可以吗?
#可不可以
pattern = ' data-src="(.+?)"'
#pattern1 = ' data-src="(.+?)"'
content = open('p3.txt').read()
result = findall(pattern, content)
#print result
for index, item in enumerate(result):
if not str(item).find('png')==-1:
data = urlopen(str(item)).read()
modifyip('p3.txt',' data-src="'+str(item),'<br /><br /><br /><img src="'+str(index)+'.png" /><br /><br /><br />')
f = file(str(index)+'.png',"wb")
f.write(data)
f.close()
if not str(item).find('jpeg')==-1:
data = urlopen(str(item)).read()
modifyip('p3.txt',' data-src="'+str(item),'<br /><br /><br /><img src="'+str(index)+'.jpg" /><br /><br /><br />')
f = file(str(index)+'.jpg',"wb")
f.write(data)
f.close()
if not str(item).find('gif')==-1:
data = urlopen(str(item)).read()
modifyip('p3.txt',' data-src="'+str(item),'<br /><br /><br /><img src="'+str(index)+'.gif" /> <br /><br /><br />')
f = file(str(index)+'.gif',"wb")
f.write(data)
f.close()
#os.remove('p1.htm')
#os.remove('p2.htm')
#//
#import time
import os,sys,re
import numpy as np
#from math import isnan
import pandas as pd
import jieba
import jieba.analyse
#import xlrd
#import xlwt
import codecs
#不要选择太普通的词, 否则文件太大。
#jieba 所有词
#带分页的文章
#strzk3="""网络的资源占用情况"""
#listzk1=list(jieba.cut(strzk3))
##读取文本文件内容到内存
file = codecs.open("px.txt", 'r')
content = file.read()
file.close()
segments = []
##对内存中的字符串进行分词
listzk1 = list(jieba.cut(content)) #这个时候的P3是纯净的吗?,还是包含IMG的大部分垃圾
##取中英文字符超过2个的词存入数组segments
#for seg in segs:
# if len(seg) > 1:
# segments.append(seg)
listzk = []
the_set = set()
for level in listzk1:
if level not in the_set:
the_set.add(level)
listzk.append(level)
df = pd.DataFrame(listzk,columns=['word'])
df2=df.sort_values(by=['word'],ascending=False) #这个排序结果不是按照中文拼音排序的
df2.to_csv('test.csv', sep=',',header=False,index=False) #打开文件后,再按中文拼音排序
f=open('3.txt','w+')
import shutil
for indexs in df2.index:
#print(df2['word'].iloc[indexs])
f.write(df2['word'].iloc[indexs]+chr(10))
word=df2['word'].iloc[indexs].encode('utf8')
shutil.copyfile("p.htm", "x "+'%d' %(indexs+1)+'.htm')
modifyip("x "+'%d' %(indexs+1)+'.htm',word,'<p style="color:red"><B>'+word+'</B>')
f.close()
#python怎么判断是中文字符
import re
zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
def contain_zh(word):
'''
判断传入字符串是否包含中文
:param word: 待判断字符串
:return: True:包含中文 False:不包含中文
'''
word = word#.decode()
global zh_pattern
match = zh_pattern.search(word)
return match
for indexs in df2.index:
word1=df2['word'].iloc[indexs].encode('utf8')
modifyip('p3.txt',word1,word1+" ")
#先执行上面的操作,再执行下面的
#怎么解决图片无法正常显示的问题
modifyip('p3.txt','/ >','/>')
modifyip('p3.txt',' . png','.png')
modifyip('p3.txt',' . jpg','.jpg')
modifyip('p3.txt',' . gif','.gif')
modifyip('p3.txt',' png','png')
modifyip('p3.txt',' jpg','jpg')
modifyip('p3.txt',' gif','gif')
modifyip('p3.txt',' .png','.png')
modifyip('p3.txt',' .jpg','.jpg')
modifyip('p3.txt',' .gif','.gif')
html3="""
<html>
<head>
<title></title>
<head>
<body>
"""
html4="""
</body>
</html>
"""
ff=open('p3.txt','a+')
jjj=ff.read()
ff.close
ff=open('p3.txt','w+')
ff.write(html3)
ff.write(jjj)
ff.write(html4)
ff.close()
#为什么图片无法正常显示
#现在一切都正常了, 剩下的就是要把图片的残余部分清理干净。
import sys
reload(sys)
sys.setdefaultencoding("gbk")
from re import findall
import requests
import re
from urllib import urlopen
from bs4 import BeautifulSoup
def modifyip(tfile,sstr,rstr):
try:
lines=open(tfile,'r').readlines()
flen=len(lines)-1
for i in range(flen):
if sstr in lines[i]:
lines[i]=lines[i].replace(sstr,rstr)
open(tfile,'w').writelines(lines)
except Exception,e:
print e
url = 'http://mp.weixin.qq.com/s/l9J5CLSR2Oo83QwQx119vw'
content = urlopen(url).read()
f1 = file('p.htm','wb')
f1.write(content)
f1.close()
#f2=file('pic.txt','wb')
import os
import math
altxt = open('p.htm').read()
pattern = '<script(.+?)</script>'
#out = re.sub(pattern,' ', altxt)
listpp=re.split(r'<script|</script>|<style>|</style>',altxt)
ff=open('p1.htm','w+')
ff.close
f1 = file('p1.htm','a+')
for i in listpp:
print listpp.index(i)
if (listpp.index(i)+2)%2 == 0:
print i
f1.write(i)
f1.close()
#这上面是清除script
#目前对htm分词已经成功,剩下的就是在HTM中置入图片
#首先要把图片插入,这个用
#首先分词会把特殊的字符处理掉
#需要在分词之后再添加
#怎么处理分词之后的文本,怎样拆分分词之后的文本
#在找到图片网址之后,该怎样处理呢?
#增加什么特殊的字符,可以便于后期的处理呢
#这里只是修改p.htm,如果能记下文件名,后期就便于处理了。根据图片的文件名,找到相应位置,然后把文件拆分成不同的字符串,然后再合并就可以了
#这个思路不错,核心在于拆分文件。
# 对于t,写入P3.TXT,
#在这之后,就已经清除掉了图片网址,能不能忽略网址呢?
#在后面才进行图片的下载和处理,这之前还没有处理。 可以把图片处理放到前面吗?
#这下面已经用到了分割字符,只需要把这个分割字符,按照图片网址分割开,分别保存即可。
#仔细弄清楚它是怎么分割的。
#打开文件,
#pattern = '<script(.+?)</script>' 这句似乎没用
#listpp=re.split(r'<|>',altxt) 分割后,生成一个listapp
#
ff=open('p2.htm','w+')
ff.close
altxt = open('p1.htm').read()
#pattern = '<script(.+?)</script>'
#out = re.sub(pattern,' ', altxt)
listpp=re.split(r'<|>',altxt)
f2 = file('px.txt','a+')
for i in listpp:
#print listpp.index(i)
if (listpp.index(i)+2)%2 == 0:
#print i
f2.write(i+chr(10))
f1.close()
f1 = file('p2.txt','a+')
for i in listpp:
#print listpp.index(i)
if (listpp.index(i)+2)%2 == 0:
#print i
f1.write(i+chr(10))
if not i.find('data-src="')==-1 :#如果在i中包含img就保留,这样就可以包含图片了, 注意看一下,这种思路是对的。 只需要继续试下去即可。看一下原始格式。
jx=i.find('data-src="') #位置
sx=i[jx+10:] #截取后半段
jx1=sx.find('"')
wi=i[jx-1:jx+10+jx1+1]
f1.write(wi+chr(10))
#垃圾是从这个地方开始出现的,不要写i,而要裁剪。 只留下最重要的部分。怎么对i进行裁剪呢?首先它是一个字符串,从data-src开始裁剪,到第一个双引号的位置直到结束
#
f1.close()
#这个时候的P3是纯净的吗?,还是包含IMG的大部分垃圾
#为什么会写好几遍呢?
#这个地方生成的P2.TXT, 是巧妙地, 但是分词后会出问题。所以分词之前,就要妥善处理
#
"""
for i in range(1):
with open('p2.htm','r+') as f:
t = f.read()
t = t.replace('/head', '')
t = t.replace('head', '')
t = t.replace('/body', '')
t = t.replace('body', '')
t = t.replace('/html', '')
t = t.replace('html', '')
#t = t.replace(' ', '')
#读写偏移位置移到最开始处
f.seek(0, 0)
f.write(t)
"""
def delblankline(infile,outfile):
infopen = open(infile,'r')
outfopen = open(outfile,'w')
lines = infopen.readlines()
for line in lines:
if line.split():
outfopen.writelines(line)
else:
outfopen.writelines("")
infopen.close()
outfopen.close()
ff=open('p3.txt','w+')
ff.close
delblankline("p2.txt","p3.txt")
#这一部分是剔除空行
#先分割再插入,可以吗?
#可不可以
pattern = ' data-src="(.+?)"'
#pattern1 = ' data-src="(.+?)"'
content = open('p3.txt').read()
result = findall(pattern, content)
#print result
for index, item in enumerate(result):
if not str(item).find('png')==-1:
data = urlopen(str(item)).read()
modifyip('p3.txt',' data-src="'+str(item),'<br /><br /><br /><img src="'+str(index)+'.png" /><br /><br /><br />')
f = file(str(index)+'.png',"wb")
f.write(data)
f.close()
if not str(item).find('jpeg')==-1:
data = urlopen(str(item)).read()
modifyip('p3.txt',' data-src="'+str(item),'<br /><br /><br /><img src="'+str(index)+'.jpg" /><br /><br /><br />')
f = file(str(index)+'.jpg',"wb")
f.write(data)
f.close()
if not str(item).find('gif')==-1:
data = urlopen(str(item)).read()
modifyip('p3.txt',' data-src="'+str(item),'<br /><br /><br /><img src="'+str(index)+'.gif" /> <br /><br /><br />')
f = file(str(index)+'.gif',"wb")
f.write(data)
f.close()
#os.remove('p1.htm')
#os.remove('p2.htm')
#//
#import time
import os,sys,re
import numpy as np
#from math import isnan
import pandas as pd
import jieba
import jieba.analyse
#import xlrd
#import xlwt
import codecs
#不要选择太普通的词, 否则文件太大。
#jieba 所有词
#带分页的文章
#strzk3="""网络的资源占用情况"""
#listzk1=list(jieba.cut(strzk3))
##读取文本文件内容到内存
file = codecs.open("px.txt", 'r')
content = file.read()
file.close()
segments = []
##对内存中的字符串进行分词
listzk1 = list(jieba.cut(content)) #这个时候的P3是纯净的吗?,还是包含IMG的大部分垃圾
##取中英文字符超过2个的词存入数组segments
#for seg in segs:
# if len(seg) > 1:
# segments.append(seg)
listzk = []
the_set = set()
for level in listzk1:
if level not in the_set:
the_set.add(level)
listzk.append(level)
df = pd.DataFrame(listzk,columns=['word'])
df2=df.sort_values(by=['word'],ascending=False) #这个排序结果不是按照中文拼音排序的
df2.to_csv('test.csv', sep=',',header=False,index=False) #打开文件后,再按中文拼音排序
f=open('3.txt','w+')
import shutil
for indexs in df2.index:
#print(df2['word'].iloc[indexs])
f.write(df2['word'].iloc[indexs]+chr(10))
word=df2['word'].iloc[indexs].encode('utf8')
shutil.copyfile("p.htm", "x "+'%d' %(indexs+1)+'.htm')
modifyip("x "+'%d' %(indexs+1)+'.htm',word,'<p style="color:red"><B>'+word+'</B>')
f.close()
#python怎么判断是中文字符
import re
zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
def contain_zh(word):
'''
判断传入字符串是否包含中文
:param word: 待判断字符串
:return: True:包含中文 False:不包含中文
'''
word = word#.decode()
global zh_pattern
match = zh_pattern.search(word)
return match
for indexs in df2.index:
word1=df2['word'].iloc[indexs].encode('utf8')
modifyip('p3.txt',word1,word1+" ")
#先执行上面的操作,再执行下面的
#怎么解决图片无法正常显示的问题
modifyip('p3.txt','/ >','/>')
modifyip('p3.txt',' . png','.png')
modifyip('p3.txt',' . jpg','.jpg')
modifyip('p3.txt',' . gif','.gif')
modifyip('p3.txt',' png','png')
modifyip('p3.txt',' jpg','jpg')
modifyip('p3.txt',' gif','gif')
modifyip('p3.txt',' .png','.png')
modifyip('p3.txt',' .jpg','.jpg')
modifyip('p3.txt',' .gif','.gif')
html3="""
<html>
<head>
<title></title>
<head>
<body>
"""
html4="""
</body>
</html>
"""
ff=open('p3.txt','a+')
jjj=ff.read()
ff.close
ff=open('p3.txt','w+')
ff.write(html3)
ff.write(jjj)
ff.write(html4)
ff.close()
#为什么图片无法正常显示
#现在一切都正常了, 剩下的就是要把图片的残余部分清理干净。