大数据分析作业

大数据 分析 作业| 2022-09-05
大数据分析作业

#1.爬数据

fromosimportwrite

importrequests#获取网页源码:

importurllib.request

frombs4importBeautifulSoup#用来解析解析HTML文档

importtime

#2.数据预处理

importjieba#分词、去停用词等

#3.文本特征工程(向量化)

fromsklearn.feature_extraction.textimportCountVectorizer

fromsklearn.feature_extraction.textimportTfidfTransformer

importpandasaspd

importnumpyasnp

#1.爬数据

print("***n***n***n这是一个爬虫,正在爬取百度贴吧的一个内容,请耐心等候:。。。")

#设置保存数据的文件

f=open(r'C:UsersDELLDesktop大数据大数据实验.txt','a+',encoding='utf-8')#打开文件,a+表示在文件末尾追加

end_time=time.strftime('%Y-%m-%d%H:%M:%S',time.localtime(time.time()))#当前的时间

#f.write("【时间:"+end_time+"】n【标题】阿巴阿巴"+'n')

#获取网页源码:(得到解析HTML文档)(请在联网情况下进行)

#url="https://tieba.baidu.com/f?ie=utf-8&kw=%E7%89%A9%E7%90%86"

url="https://tieba.baidu.com/f?kw=%E7%9D%A1%E8%A7%89&fr=fenter&prequery=%E7%9D%A1%E8%A7%89%E9%81%87%E5%88%B0%E9%9A%9C%E7%A2%8D"

r=requests.get(url)

print("状态码:",r.status_code,"若显示2xx表示连接成功,可继续下一步")#若打印2xx表示连接成功,可继续下一步

print("网页编码方式:",r.apparent_encoding)

html=urllib.request.urlopen(url).read()#得到html文本

#print(html)

#解析HTML文档

soup=BeautifulSoup(html,"html.parser")#soup=BeautifulSoup(html,"lxml"),是lxml的问题,在报错代码中把函数参数中所有的"lxml"改成"html.parser"

print(soup.prettify())#.prettify()打印格式,就是将soup中的'n'变为换行

print("------------------------------------------------------------------------------")

#从网页中提取想要的数据所在的节点

all=soup.find_all("a",class_="j_th_tit")#查找标签中的class_="j_th_tit",find_all返回数组

#print(all)

print("------------------------------------------------------------------------------")

ALL=str(all).split('</a>')#字符串化,去掉了</a>

ALL.pop()

print(ALL)

print('--------------------------------------------------------------------------------------------------------------')

L=[]

forsinALL:

L.append(s.split('title="')[1])#split()返回的是一个数组,L.append()添加的是title后面的东西

print(L)

i=0

forsinL:

q,w=s.split('">',1)#q='>'前面的字符串,w='>'后面的字符串,逗号后面1表示分割一次

#i+=1

#f.write('【标题'+str(i)+'】:'+q+'n')

f.write(q+'n')

f.close()

#在这一步,我们在文件保存了我们爬到的数据,,放在了'C:UsersDELLDesktop大数据大数据实验.txt'

#2.去停用词、名词等(文本预处理)步骤:处理---》读文件-》处理-》写入文件

#(1)去除非中文字符,将处理后的文档写入大数据实验文本数据0

f=open(r'C:UsersDELLDesktop大数据大数据实验.txt','r',encoding='utf-8',errors='ignore')#将文本格式编码为utf-8,防止编码错误

fw=open(r'C:UsersDELLDesktop大数据大数据实验文本数据0.txt','w',encoding='utf-8',errors='ignore')

forlineinf:#逐行处理

constr=''#记录每行处理后的数据

forucharinline:

ifuchar>=u'u4e00'anduchar<=u'u9fa5':#是中文字符

ifuchar!='':#去除空格

constr+=uchar

fw.write(constr+'n')#写入处理后的数据,每行以空格隔开

f.close()

fw.close()#不要忘记

#2.去停用词、名词等(文本预处理)步骤:处理---》读文件-》处理-》写入文件

#(1)去除非中文字符,将处理后的文档写入大数据实验文本数据0

f=open(r'C:UsersDELLDesktop大数据1疾风剑豪吧.txt','r',encoding='utf-8',errors='ignore')#将文本格式编码为utf-8,防止编码错误

fw=open(r'C:UsersDELLDesktop大数据1大数据实验文本数据0.txt','w',encoding='utf-8',errors='ignore')

forlineinf:#逐行处理

constr=''#记录每行处理后的数据

forucharinline:

ifuchar>=u'u4e00'anduchar<=u'u9fa5':#是中文字符

ifuchar!='':#去除空格

constr+=uchar

fw.write(constr+'n')#写入处理后的数据,每行以空格隔开

f.close()

fw.close()#不要忘记

#准备去停用词

#(2)-------------------创建放停用词的数组stopwords[]

stopwords=[]

stopwords=[]#注意,去停用词表中的标点符号应该用行隔开,不能用空格隔开,否则forwordinopen会将它看为一个字符串,数组中只有一个元组

forwordinopen(r"C:UsersDELLDesktop大数据1去停用词表.txt","r",encoding='utf-8'):#这里让我把去"停用词表.txt"另存为是编码方式改为wtf-8才不报错

stopwords.append(word.strip())

#-------------------创建结束-------------------------------------

#去停用词(3)

jieba.load_userdict(r'C:UsersDELLDesktop大数据1词典.txt')#添加词典

print('-----------------------------去停用词然后将去停用词后的字符串存到大数据实验文本数据.txt----------------------------------------------------')

f2=open(r'C:UsersDELLDesktop大数据1大数据实验文本数据0.txt','r+',encoding='utf-8')#r+:表示用于读写

f1=open(r'C:UsersDELLDesktop大数据1大数据实验文本数据.txt','a',encoding='utf-8')#a:表示用于追加

L=f2.readlines()#f.readlines()返回一个数组,一行为一个元素

forl1inL:#每一行进行分词->取停用词->写入大数据实验文本数据.txt

a=""

seg_lists=jieba.cut(l1,cut_all=True)#全模式,jieba.cut()返回一个可迭代的generator

forseg_listinseg_lists:#一个for循环,每一行的seg_list->a

#print(seg_list)

ifseg_list.encode("utf-8")notinstopwords:

a+=seg_list+''

f1.write(a)#写入大数据实验文本数据.txt

f2.close()

f1.close()

#3.文本特征工程(统计每个词出先的频率)

#(1)(完成词频向量化)

#打开要向量化的文件

vectorizer_向量化=CountVectorizer()

f2=open(r'C:UsersDELLDesktop大数据1大数据实验文本数据.txt','r+',encoding='utf-8')

corpus=f2.readlines()#L2是一个数组