爬取校园新闻首页的新闻

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
        href=news.select('a')[0].attrs['href']

        resd=requests.get(href)
        resd.encoding='utf-8'
        soupd=BeautifulSoup(resd.text,'html.parser')

        ts = soupd.select('#content')[0].text
        infod=soupd.select('.show-info')[0].text
        dt=infod.lstrip('发布时间:')[:19]
        dati = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
        ther=infod[infod.find('作者:'):infod.find('审核:')].split()[0].lstrip('作者:')
        source = infod[infod.find('来源:'):].split()[0].lstrip('来源:')
        photo = infod[infod.find('摄影:'):].split()[0].lstrip('摄影:')
        print(dati, dati.strftime('%Y/%m/%d'))
        #print(dt,ther,source,)

  

原文地址:https://www.cnblogs.com/polvem/p/8711718.html