zoukankan      html  css  js  c++  java
  • 【大数据】爬取全部的校园新闻

    作业来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2941

    1.从新闻url获取新闻详情: 字典,anews

    2.从列表页的url获取新闻url:列表append(字典) alist

    3.生成所页列表页的url并获取全部新闻 :列表extend(列表) allnews

    *每个同学爬学号尾数开始的10个列表页

    4.设置合理的爬取间隔

    import time

    import random

    time.sleep(random.random()*3)

    5.用pandas做简单的数据处理并保存

    保存到csv或excel文件 

    newsdf.to_csv(r'F:duym爬虫gzccnews.csv')

    保存到数据库

    import sqlite3
    with sqlite3.connect('gzccnewsdb.sqlite') as db:
        newsdf.to_sql('gzccnewsdb',db)

    代码实现:

    # 获取一篇新闻的全部信息
    import re
    import requests
    import time
    import random
    import pandas as pd
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    # 获取新闻id
    def newsnum(url):
        newsid = re.match('http://news.gzcc.cn/html/2015/xiaoyuanxinwen_(.*)/(.*).html', url).group(2)
        return newsid
    
    # 发布时间:datetime类型
    def newstime(soup):
        newsdate = soup.select('.show-info')[0].text.split()[0].split(':')[1]
        newstime = soup.select('.show-info')[0].text.split()[1]
        time = newsdate + ' ' + newstime
        time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
        return time
    
    # 获取点击次数
    def getClick(id):
        clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(id)
        res = requests.get(clickUrl)
        click = res.text.split('.html')[-1].lstrip("('").rstrip("');")
        return click
    
    # 获取新闻详情
    def getDetails(url):
        res = requests.get(url)
        res.encoding = 'utf-8'
        soup = BeautifulSoup(res.text, 'html.parser')
        anews = {}
        id = newsnum(url);  #新闻id
        anews['新闻编号'] = id;
        anews['标题'] = soup.select('.show-title')[0].text;
        anews['发布时间'] = str(newstime(soup));
        anews['作者'] = re.match('作者:(.*)',soup.select('.show-info')[0].text.split()[2]).group(1);
        #anews['来源'] = re.match('来源:(.*)', soup.select('.show-info')[0].text.split()[3]).group(1);
        anews['点击次数'] = getClick(id);
        anews['url'] = url;
        # anews['内容'] = soup.select('.show-content p')[0].text;
        # print(anews)
        return anews
    
    # 从列表页的url获取新闻url
    def getUrl(url):
        res = requests.get(url)
        res.encoding = 'utf-8'
        soup = BeautifulSoup(res.text, 'html.parser')
        alist = []
        for i in range(len(soup.select('.news-list')[0].select('a'))):
            list = soup.select('.news-list')[0].select('a')[i]['href']
            alist.append(list)
        return alist
    
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/74.html'
    allnews = []
    for i in getUrl(url):
        allnews.append(getDetails(i));
        time.sleep(random.random() * 3);
    
    # 生成csv文件
    newsdf=pd.DataFrame(allnews)
    newsdf.to_csv(r'gzccnews.csv',encoding = 'utf-8')
    
    #生成数据库文件
    import sqlite3
    with sqlite3.connect('gzccnewsdb.sqlite') as db:
        newsdf.to_sql('gzccnewsdb',db)
    
    print(allnews)

    生成的csv文件:

    生成的sqlite文件:

  • 相关阅读:
    angularjs学习笔记—工具方法
    js日期格式转换的相关问题探讨
    vue路由原理剖析
    如何减少UI设计师产品与前端工程师的沟通成本
    前端优化带来的思考,浅谈前端工程化
    前端入门方法
    自写juqery插件实现左右循环滚动效果图
    前端大综合
    前端收集
    如何在代码中减少if else语句的使用
  • 原文地址:https://www.cnblogs.com/Richard-V/p/10696639.html
Copyright © 2011-2022 走看看