zoukankan      html  css  js  c++  java
  • 爬取校园新闻首页的新闻

    import requests
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            #d = news.select('.news-list-info')[0].contents[0].text
            t = news.select('.news-list-title')[0].text #标题
            m = news.select('.news-list-description')[0].text #内容
            a = news.select('a')[0].attrs['href']#链接
    
            print(t,a,m)
    
            resd = requests.get(a)
            resd.encoding = 'utf-8'
            soupd = BeautifulSoup(resd.text, 'html.parser')
    
            c = soupd.select('#content')[0].text
            print(c) #正文
            info = soupd.select('.show-info')[0].text
            print(info)
            dt = info.lstrip('发布时间:')[:19]
            dati = datetime.strptime(dt,'%Y-%m-%d %H:%M:%S')
            sh = info[info.find('来源:'):].split()[0].lstrip('来源:')
            wr = info[info.find('作者:'):info.find('审核:')].lstrip('作者:').split()[0]
            ph = info[info.find('摄影:'):].split()[0].lstrip('摄影:')
            print(dati,wr,sh,ph)
            break
    

  • 相关阅读:
    Ajax基础知识详解
    php 基础语法整理
    06 js-递归
    原生js 实现瀑布流布局
    解决高度坍塌问题
    jQuery动画效果
    jQuery增删元素类名
    JQueryTab栏切换(important!)
    jQuery排他思想(important!)
    jQuery设置元素样式
  • 原文地址:https://www.cnblogs.com/cgq520/p/8710038.html
Copyright © 2011-2022 走看看