• 爬取校园新闻首页的新闻


    1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。

    2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。

    3. 将其中的发布时间由str转换成datetime类型。

    4. 将完整的代码及运行结果截图发布在作业上。

    import requests
    from bs4 import BeautifulSoup
    
    url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    print(soup.select('.news-list')[0].select('li'))
    for new in soup.select('.news-list')[0].select('li'):
        # 标题
        ti = new.select('.news-list-title')[0].text
        # 时间
        tim = new.select('span')[0].text
        # 来源
        source = new.select('span')[1].text
        print('标题:'+ ti + ' 时间:'+ tim + ' 来源:'+ source)
        # 链接
        a = new.select('a')[0].attrs['href']
        resd = requests.get(a)
        resd.encoding = 'utf-8'
        soupd = BeautifulSoup(resd.text, 'html.parser')
        # 正文
        print('正文:')
        content = soupd.select('#content')[0].text.split()
        for c in content:
            print(c)
    
    info = '发布时间:2018-04-01 11:57:00      作者:陈流芳  审核:权麟春  来源:马克思主义学院'
    dt = info.lstrip('发布时间:')[:15]
    print(dt)
    print(info.find('作者'))#找到‘时间’位置
    print(info[info.find('审核'):].split()[0].lstrip('审核:'))
    print(info[info.find('作者:'):info.find('审核:')])
    
    from datetime import datetime
    now = datetime.now()
    print(now)
    print(tim)
    da = datetime.strptime(tim, '%Y-%m-%d')
    print(da)
    n = now.strftime('%y/%m/%d')
    print(n)
    

      

  • 相关阅读:
    简易聊天客户端程序
    java 多线程使用方法及Socket的使用
    跟着音乐节奏随机的产生不同颜色形状的图形
    移动小圆圈
    在一个frame设置四个组件
    触发按钮改变panel面板上的小圆圈颜色
    借助bootstrap框架模仿airbnb写的网页
    攻击DotCom小游戏
    mit java open course assignment #4
    FPS 游戏实现GDI透视
  • 原文地址:https://www.cnblogs.com/2647409627qq/p/8710522.html
Copyright © 2020-2023  润新知