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

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

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

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

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

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

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

    我的主题:爬取腾讯体育-NBA

    按F12使用开发者工具分析腾讯体育NBA新闻网站的结构,找到需要的类名字“list01”,爬取新闻标题和链接。

    点开一条新闻链接,继续分析,找到需要的文章内容类名,然后进行分词分析。

    在实现的过程中遇到过不少问题,例如字符集charset,一开始使用了utf-8,结果出现乱码,后经查看网站结构解决,本网站使用的charset是gb2312。

    还有一些调用参数问题等等。还有词云的字体问题,通过查询一些通用字体格式,最终实现。

    词云效果图下:

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

    import requests
    from bs4 import BeautifulSoup
    import re
    import jieba
    # from PIL import Image,ImageSequence
    # import numpy as np
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud
    
    # 将词云写入到文件
    def writeFile(keynews):
        f = open('cgpword.txt', 'a', encoding='utf-8')
        for word in keynews:
            f.write(" "+word)
        f.close()
    
    def getWordCloud():
        keywords = open('cgpword.txt', 'r', encoding='utf-8').read()
        wc = WordCloud(font_path='C:windows/font/simkai.ttf', background_color='white',
                       max_words=150).generate(keywords).to_file("cgp1.jpg")
        plt.imshow(wc)
        plt.axis('off')
        plt.show()
    
    #对新闻内容进行分词分析
    def getnewsdetail(newsurl):
        resDescript = requests.get(newsurl)
        resDescript.encoding = "gb2312"
        soupDescript = BeautifulSoup(resDescript.text, 'html.parser')
        to = len(soupDescript.select(".text"))
        content = ''
        for p in range(0, to):
            content += soupDescript.select('.text')[p].text + '
    '
        # print(content)
        words = jieba.lcut(content)
        wcdict = {}
        keynews = []
        for i in set(words):
            wcdict[i] = words.count(i)
            delete = {'', '', '', '', '已经', '', '', '', '没有', '', '他们', '', '', '什么', '', '',
                      '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                      '', '', '', '', '', '', '', '', '',  '', '', '', '', '', '', '', '', '', '',
                      '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                      '', '', '', '', '', ' ', '', '-', '
    ', '', '', '', '', '', '', '', '', '', '.', '', ''}
        for i in delete:
            if i in wcdict:
                del wcdict[i]
        sort_word = sorted(wcdict.items(), key=lambda d: d[1], reverse=True)  # 排序
        for dict in sort_word:
            keynews.append(dict[0])
        writeFile(keynews)
    
    
    #获取该网站的所有新闻标题和链接
    def getnewslist(newsurl):
        res = requests.get(newsurl)
        res.encoding = 'gb2312'
        soup = BeautifulSoup(res.text, 'html.parser')
        for newsList in soup.select('.list01')[0].select('li'):
            title = newsList.select('a')[0].text
            newsurl = newsList.select('a')[0]['href']
            print('
    标题:{0}
    新闻链接:{1}
    '.format(title, newsurl))
            getnewsdetail(newsurl)
    
    
    url = "http://sports.qq.com/l/basket/original/qqinterview/list20150821155646.htm"
    resn = requests.get(url)
    resn.encoding = 'utf-8'
    soupn = BeautifulSoup(resn.text,'html.parser')
    getnewslist(url)
    
    
    for i in range(1, 3):
        if (i == 1):
            getnewslist(url)
        else:
            newsurl = "http://sports.qq.com/l/basket/original/qqinterview/list20150821155646_{}.htm".format(i)
            getnewslist(newsurl)
    getWordCloud()
  • 相关阅读:
    CSS书写技巧(转)
    OutputCache祥解
    [推荐/译文/转]从底层角度看ASP.NETA lowlevel Look at the ASP.NET Architecture
    [转]ASP.NET页面解析
    .NET开发中你可能会用到的常用方法总结
    asp.net 页面清除缓存
    ASP.NET内部原理(HttpHandler和HttpModule)
    .NET开发中你可能会用到的常用方法总结(2)
    C#解析HTML
    如何理解IIS 7的两种应用程序池的管道模式(Managed Pipeline Mode)
  • 原文地址:https://www.cnblogs.com/chenguangpeng/p/8973475.html
Copyright © 2011-2022 走看看