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


    import requests
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    url = "http://news.gzcc.cn/html/xiaoyuanxinwen/"
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    a = soup.select('li')
    
    
    for news in a:
        if len(news.select('.news-list-title'))>0:
            t = news.select('.news-list-title')[0].text
            dt = news.select('.news-list-info')[0].contents[0]
            dd = news.select('.news-list-info')[0].contents[1].text
            a1 = news.select('a')[0].attrs['href']
            res1 = requests.get(a1)
            res1.encoding = 'utf-8'
            soup1 = BeautifulSoup(res1.text, 'html.parser')
            content = soup1.select("#content")[0].text
            about = soup1.select('.show-info')[0].text
            time = about.lstrip('发布时间:')[:19]
            s = datetime.strftime(time,'%Y-%m-%d %H:%M:S%')
            now = datetime.now()
            type(now)
            now.strftime('%Y-%m-%d %H:%M:S%')
            if about.find('来源:')>0:
                origin = about[about.find('来源:'):].split()[0].lstrip('来源:')
            if about.find('作者:')>0:
                writer = about[about.find('作者:'):].split()[0].lstrip('作者:')
            if about.find('摄影:')>0:
                photograph = about[about.find('摄影:'):].split()[0].lstrip('摄影:')
            print(t,dt,dd,a1,now,origin,writer,photograph)
    str = '2018-03-30 17:10:12 '
    datetime.strptime(str,'%Y-%m-%d %H:%M:%S ')
    print('
    ',str)
    

      

  • 相关阅读:
    PHPstorm配置xdebug问题小记
    PHP 实现遍历出目录及其子文件
    localStorage存、取数组
    关于用户体验
    PHP实现导出Excel文件
    js将一位数组分割成每三个一组
    vue 动态绑定背景图片
    父组件传值给子组件
    数组字符串 转化成 对象
    Vuex 页面刷新后store保存的数据会丢失 取cookie值
  • 原文地址:https://www.cnblogs.com/hano/p/8696779.html
Copyright © 2020-2023  润新知