import requests
from lxml import etree
import jieba
import re
url="https://www.qjnu.edu.cn/channels/9260.html"
def Pachong(u,i):
a=requests.get(u)
b=a.content.decode()
c=etree.HTML(b)
d=c.xpath("//h4/a/text()")
with open("学校要闻标题.txt",'at',encoding='utf-8') as f:
f.writelines(d)
d1=c.xpath("//span/text()")
for i in d1:
result=re.findall('[\u4e00-\u9fa5]+',i)
result=''.join(result).replace('作者','作者:')
with open("学校要闻发布者.txt",'at',encoding='utf-8') as ff:
ff.writelines(result)
Pachong(url,1)
n=eval(input("请输入需要爬取的网页数:"))
for i in range(2,n+1):
u=url[:-5]+'_'+str(i)+url[-5:]
Pachong(u,i)
def jieba1(name):
fo=open(name+'发帖量排名'+'.txt','w',encoding='utf-8')
with open(name+'.txt','r',encoding='utf-8') as f:
a=f.read()
words=jieba.lcut(a)
d={}
for word in words:
d[word]=d.get(word,0)+1
count=list(d.items())
count.sort(key=lambda x:x[1],reverse=True)
for i in range(len(count)):
key,value=count[i]
fo.write("{}:{}\n".format(key,value))
fo.close()
jieba1('学校要闻发布者')