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

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

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

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

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

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

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

    爬取京东某一本书的评价数据,并写入到jd.txt里面:

    file = open('jd.txt','w')
    def crawlProductComment(url,page):
        html = urllib.request.urlopen(url).read().decode('gbk')
        jsondata = html[26:-2]
        # print(jsondata)
        data = json.loads(jsondata)
        for i in data['comments']:
            commentTime = i['creationTime']
            content = i['content']
            print("时间:{}".format(commentTime))
            file.write("{}".format(commentTime) + '
    ')
            print("评论:{}".format(content))
            file.write("{}".format(content) + '
    ')
            print("-----------------------------")
    
    
    for i in range(0,30):
        print("正在获取第{}页评论数据!".format(i+1))
        url = 'https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv6785&productId=11879970&score=0&sortType=5&page=' + str(i) +'&pageSize=10&isShadowSku=0&fold=1'
        crawlProductComment(url,i)
    file.close()
    

      

    生成词云:

    lyric= ''
    f=open('jd.txt','r')
    for i in f:
        lyric+=f.read()
    
    
    result=jieba.analyse.textrank(lyric,topK=50,withWeight=True)
    keywords = dict()
    for i in result:
        keywords[i[0]]=i[1]
    print(keywords)
    
    image= Image.open('ii.jpg')
    graph = np.array(image)
    wc = WordCloud(font_path='./fonts/simhei.ttf',background_color='White',max_words=50,mask=graph)
    wc.generate_from_frequencies(keywords)
    image_color = ImageColorGenerator(graph)
    plt.imshow(wc)
    plt.imshow(wc.recolor(color_func=image_color))
    plt.axis("off")
    plt.show()
    wc.to_file('dd.jpg')
    

    文本数据图:

    词云图:

      

  • 相关阅读:
    Jquery实现Gridview全选功能
    SQL Server日期计算
    避免表格table被撑开变形的CSS代码实例
    oracle游标使用
    最短路径算法及应用
    Jquery实现GridView隔行变色,鼠标经过变色,单击或者选中变色
    【项目】项目109
    【项目】项目107
    【项目】项目111
    【项目】项目110
  • 原文地址:https://www.cnblogs.com/18128319239F/p/8919980.html
Copyright © 2011-2022 走看看