zoukankan      html  css  js  c++  java
  • 爬虫大作业

    1.选一个自己感兴趣的主题(所有人不能雷同)。

    我选择了虎扑nba的体育新闻页面,与校园新闻版面类似,爬去50页

    2.用python 编写爬虫程序,从网络上爬取相关主题的数据。

     利用所学知识,导入要用的类

    import requests
    from bs4 import BeautifulSoup
    import jieba

    审查元素,获取网页内容

     

    3.对爬了的数据进行文本分析,生成词云。

    for i in range(2294450,2294500):
    pages = i;
    nexturl = 'https://voice.hupu.com/nba/%s.html' % (pages)
    reslist = requests.get(nexturl)
    reslist.encoding = 'utf-8'
    soup_list = BeautifulSoup(reslist.text, 'html.parser')
    for news in soup_list.find_all('div',class_='artical-main-content'):
    print(news.text)
    f = open('hpnba.txt', 'a', encoding='utf-8')
    f.write(news.text)
    f.close()
    def changeTitleToDict():
    f = open("hpnba.txt", "r", encoding='utf-8')
    str = f.read()
    stringList = list(jieba.cut(str))
    delWord = {"+", "/", "(", ")", "【", "】", " ", ";", "!", "、"}
    stringSet = set(stringList) - delWord
    title_dict = {}
    for i in stringSet:
    title_dict[i] = stringList.count(i)
    print(title_dict)
    return title_dict

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

    这是爬取的新闻文字内容,存放在hpnba.txt中

    这是我的词云的背景图片,生成的大致样式。

     

    词云如上

    5.写一篇完整的博客,描述上述实现过程、遇到的问题及解决办法、数据分析思想及结论。

    遇到的问题就是,我原本想生成五角星的词云,找了两张图片,都不行,后来换了一张小猪,就可以了,原因目前不知道。

    6.最后提交爬取的全部数据、爬虫及数据分析源代码。

    import requests
    from bs4 import BeautifulSoup
    import jieba
    from PIL import Image,ImageSequence
    import numpy as np
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud,ImageColorGenerator
    for i in range(2294450,2294500):
            pages = i;
            nexturl = 'https://voice.hupu.com/nba/%s.html' % (pages)
            reslist = requests.get(nexturl)
            reslist.encoding = 'utf-8'
            soup_list = BeautifulSoup(reslist.text, 'html.parser')
            for news in soup_list.find_all('div',class_='artical-main-content'):
                print(news.text)
                f = open('hpnba.txt', 'a', encoding='utf-8')
                f.write(news.text)
                f.close()
    def changeTitleToDict():
        f = open("hpnba.txt", "r", encoding='utf-8')
        str = f.read()
        stringList = list(jieba.cut(str))
        delWord = {"+", "/", "", "", "", "", " ", "", "", ""}
        stringSet = set(stringList) - delWord
        title_dict = {}
        for i in stringSet:
            title_dict[i] = stringList.count(i)
        print(title_dict)
        return title_dict
    
    
    # 获取上面保存的字典
    title_dict = changeTitleToDict()
    graph = np.array(title_dict)
    font = r'C:WindowsFontssimhei.ttf'
    
    
    image= Image.open('./3.jpg')
    graph = np.array(image)
    font=r'C:WindowsFontssimhei.TTF'
    wc = WordCloud(font_path=font,background_color='White',max_words=50,mask=graph)
    wc.generate_from_frequencies(title_dict)
    image_color = ImageColorGenerator(graph)
    plt.imshow(wc)
    plt.axis("off")
    plt.show()
  • 相关阅读:
    jackson 解析json含有不规则的属性的json字符串的方法
    swift入门-实现简单的登录界面
    github git.exe位置
    linux之SQL语句简明教程---IN
    怎么样才算是精通 C++?
    BZOJ2028: [SHOI2009]会场预约(set)
    BZOJ1058: [ZJOI2007]报表统计(set)
    洛谷P2391 白雪皑皑(并查集)
    BZOJ4514: [Sdoi2016]数字配对(费用流)
    BZOJ3143: [Hnoi2013]游走(期望DP 高斯消元)
  • 原文地址:https://www.cnblogs.com/BOXczx/p/8974682.html
Copyright © 2011-2022 走看看