爬取校园新闻首页的新闻

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('\n',str)

  

转载于:https://www.cnblogs.com/hano/p/8696779.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值