zoukankan      html  css  js  c++  java
  • 一个完整的大作业

    1.选一个自己感兴趣的主题。

    2.网络上爬取相关的数据。

    3.进行文本分析,生成词云。

    4.对文本分析结果解释说明。

    5.写一篇完整的博客,附上源代码、数据爬取及分析结果,形成一个可展示的成果。

    import requests
    from bs4 import BeautifulSoup
    import re
    import pandas
    from datetime import datetime 
    import random
    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    
    url = 'http://news.qq.com/l/milite/milgn/list2010122872223.htm'
    
    list =[]
    
    def getListFullMarksArticle(url):
        res = requests.get(url)
        res.encoding = 'GBK'  
        article0 = BeautifulSoup(res.text,"html.parser")  
        articleList = {}  
        for article in article0.select('.artbox_l'):     
            list.append(getAllArticle(articleList,article))
        
        
    
    def getAllArticle(articleList,article):
        
        articleList['URL'] = article.select('li')[0].select("a")[0]['href']#链接
        articleList['TITLE'] = article.select("li")[0].select("a")[0].text#标题
        articleList['DATE'] = article.select("li")[0].select("span")[0].text#日期
    
    
        url= article.select('li')[0].select("a")[0]['href']#链接
        title = article.select("li")[0].select("a")[0].text#标题
        time = article.select("li")[0].select("span")[0].text#日期
    
        print('
    
    标题',title,'
    时间',time,'
    链接',url)
        return(articleList)
    
    #循环总页数进行输出               
    for i in range(2,4):
        allUrl='http://news.qq.com/l/milite/milgn/list2010122872223_{}.htm'.format(i)
        getListFullMarksArticle(allUrl)
        
    #保存数据
    df = pandas.DataFrame(list)
    
    df.to_excel('FullArticleList.xlsx')
    
    #制作词云
    lo = open ('FullArticleList.xlsx','r',encoding='ISO-8859-1').read()
    #lo = open ('FullArticleList.xlsx','r','GBK').read()
    #lo = open ('FullArticleList.xlsx','r','GB2312').read()
    #lo = open ('FullArticleList.xlsx','r','ASCII').read()
    #lo = open ('FullArticleList.xlsx','r','ISO-8859-8').read()
    #lo = open ('FullArticleList.xlsx','r','ISO-8859-7').read()
    FullMarks = WordCloud().generate(lo)
    plt.imshow(FullMarks)
    plt.show()

    结果如下图所示:

    由于未知原因,词云出现乱码。

  • 相关阅读:
    css 左右两栏 左边固定右边自适应
    Oracle 执行长SQL
    Oracle 游标基础
    ERROR 000464: Cannot get exclusive schema lock. Either being edited or in use by another application
    MyEclipse 批量更新编码集
    使用Windows服务发布WCF服务【转载】
    JavaScript的陷阱【转载】
    JS图片切换带超链接
    新正则表达式
    asp.net垃圾代码之asp.net去掉垃圾代码,优化aspx页面性能的方法
  • 原文地址:https://www.cnblogs.com/bb437601841/p/7771151.html
Copyright © 2011-2022 走看看