• 数据结构化与保存


    1
    2
    3
    4
    f = open('content.txt', 'a', encoding ='utf - 8')
    # content为文本正文
    f.write(content)
    f.close()

    2. 将新闻数据结构化为字典的列表:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    news ={}
    # 读取新闻细节
    def getNewDetail(detail,title,description):
        resDescript = requests.get(detail,headers)
        resDescript.encoding = 'utf-8'
        soupDescript = BeautifulSoup(resDescript.text, 'html.parser')
        news['title']=soupDescript.select('.show-title')[0].text
     
        content = soupDescript.select('.show-content')[0].text  # 正文
        info = soupDescript.select(".show-info")[0].text  # info相关内容
        # 第一种方法 分离 message = info.split()
        # 第二种方法 用正则表达式
        print('标题' + ': ' + title)
        print('概要' + ': ' + description)
        print('链接' + ': ' + detail)
        print('正文' + ' :' + content)
        if(re.search("发布时间:(.*) xa0xa0 xa0xa0作者:", info) !='Null' ):
            time = re.search("发布时间:(.*) xa0xa0 xa0xa0作者:", info).group(1)
            news['time']=time
        else:news['time']="null"
        if (re.search("作者:(.*)xa0xa0审核:", info) !='Null'):
            author = re.search("作者:(.*)xa0xa0审核:", info).group(1)
            news['author']=author
            print("作者:" + author)
        else:news['author']="null"
        if (re.search("审核:(.*)xa0xa0来源:", info) !='Null'):
            right = re.search("审核:(.*)xa0xa0来源:", info).group(1)
            news['right']=right
        else:news['right']="null"
        if (re.search('来源:(.*)xa0xa0xa0xa0摄影:', info) != "null"):
            resource = re.search('来源:(.*)xa0xa0xa0xa0摄影:', info)
            news['resource'] = resource
        else:news['resource']="null"
        if (re.search("摄影:(.*)xa0xa0xa0xa0点击:", info)!="Null"):
            video = re.search("摄影:(.*)xa0xa0xa0xa0点击:", info)
            news['video']=video
        else:news['video']="null"
        count = getNewsId(detail)
        news['count']=content
        dateTime = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
        news['dataTime']=dateTime

    3. 安装pandas,用pandas.DataFrame(newstotal),创建一个DataFrame对象df.

    df = pandas.DataFrame(all_news)

    4. 通过df将提取的数据保存到csv或excel 文件。

    1
    df.to_excel('news.xlsx')

    5. 用pandas提供的函数和方法进行数据分析:

    • 提取包含点击次数、标题、来源的前6行数据
    • 提取‘学校综合办’发布的,‘点击次数’超过3000的新闻。
    • 提取'国际学院'和'学生工作处'发布的新闻。
    1
    2
    3
    4
    df[['clicks', 'title', 'source']].head(6)
    df[(df['clicks'] > 3000) & (df['source'] == '学校综合办')]
    news_info = ['国际学院', '学生工作处']
    df[df['source'].isin(news_info)]
  • 相关阅读:
    python面向对象__call__
    python的上下文管理协议
    python析构方法__del__
    next和iter可迭代协议
    __slots__ 用法
    定制自己的数据类型
    内置的Attr系列
    反射和自省
    isinstance和issubclass
    BLE 学习
  • 原文地址:https://www.cnblogs.com/SOLARLKS/p/8810051.html
Copyright © 2020-2023  润新知