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

    1. 将新闻的正文内容保存到文本文件。

    content = soup.select('.show-content')[0].text
    f = open('gzccNews.txt','a',encoding='utf8')
    f.write(content)
    f.close()

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

    • 单条新闻的详情-->字典news
    •  news = {}
       news['title'] = soupd.select('.show-title')[0].text
       news['newsUrl'] = newsUrl
       info = soupd.select('.show-info')[0].text
       time = re.search('发布时间:(.*) xa0xa0 xa0xa0作者:', info).group(1)
       news['dtime'] = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
       if info.find('作者:') > 0:
           news['author'] = info[info.find('作者:'):].split()[0].lstrip('作者:')
       else:
           news['author'] = ''
       if info.find('审核:') > 0:
           news['check'] = info[info.find('审核:'):].split()[0].lstrip('审核:')
       else:
           news['check'] = ''
       if info.find('来源:') > 0:
           news['source'] = info[info.find('来源:'):].split()[0].lstrip('来源:')
       else:
           news['source'] = ''
       if info.find('摄影:') > 0:
           news['photo'] = info[info.find('摄影:'):].split()[0].lstrip('摄影:')
       else:
           news['photo'] = ''
       news['clickCount'] = getClickCount(newsUrl)
       news['content'] = soupd.select('.show-content')[0].text
    • 一个列表页所有单条新闻汇总-->列表newsls.append(news)
    •  newsList = []
          for news in soup.select('li'):
              if len(news.select('.news-list-title'))>0:
                  a = news.a.attrs['href']
                  newsList.append(getNewDetail(a))
    • 所有列表页的所有新闻汇总列表newstotal.extend(newsls)
    • newsTotal = []
      firstUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
      newsTotal.extend(getLiUrl(firstUrl))
      
      res = requests.get(firstUrl)
      res.encoding = 'utf-8'
      soupn = BeautifulSoup(res.text,'html.parser')
      n =  int(soupn.select('.a1')[0].text.rstrip(''))//10+1
      for i in range(2,n):
          pageUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
          print('第{}页:'.format(i))
          newsTotal.extend(getLiUrl(pageUrl))
          break

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

    import pandas
    df = pandas.DataFrame(newsTotal)

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

    df.to_excel('gzccnews.xlsx')
    df.to_csv('gzccnews.xlsx')

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

    • 提取包含点击次数、标题、来源的前6行数据
    • print(df[['clickCount','title','source']].head(6))
    • 提取‘学校综合办’发布的,‘点击次数’超过3000的新闻。
    • print(df[(df['clickCount'] > 3000) & (df['source'] == '学校综合办')])
    • 提取'国际学院'和'学生工作处'发布的新闻。
    • news_info = ['国际学院','学生工作处']
      print(df[df['source'].isin(news_info)])
  • 相关阅读:
    无法导入panda包解决方法
    VUE学习笔记——基础标签,函数
    jobs指令man手册内容
    linux 部分参数的全名
    bilibiliUP数据爬取——requests库与jason库运用实例
    宝塔面板无法进入phpadmin管理数据库解决办法
    python-spider_BeautifulSoup入门实践(一)安装以及简单的抓取数据
    c++程序设计实践——银行系统
    opencv-学习笔记
    关于pipeline的一篇转载博文https://www.cnblogs.com/midhillzhou/p/5588958.html
  • 原文地址:https://www.cnblogs.com/stcy520/p/8798831.html
Copyright © 2011-2022 走看看