zoukankan      html  css  js  c++  java
  • 数据结构化与保存

    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)]
  • 相关阅读:
    快速找到由程序员到CTO发展道路上的问路石
    从大师身上反思
    你真的了解企业虚拟化吗?
    “驱网核心技术丛书”创作团队访谈
    程序员到CTO需要准备什么
    深入搜索引擎的关键——索引
    程序员到CTO必须注意的几个关键点
    微软全球MVP教你如何规划程序人生
    “碟中碟”虚拟光驱软件开发者——万春 读《寒江独钓——Windows内核安全编程 》有感
    常用jar包之commonscollection使用
  • 原文地址:https://www.cnblogs.com/SOLARLKS/p/8810051.html
Copyright © 2011-2022 走看看