爬取校园新闻首页的新闻

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

# -*- codding: UTF-8 -*-
# -*- author: WF -*-
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')

for news in soup.select('li'):
    if len(news.select('.news-list-title'))>0 : #排除为空的li
        data = news.select('.news-list-info')[0].contents[0].text #日期
        title = news.select('.news-list-title')[0].text #标题
        description = news.select('.news-list-description')[0].text #标题下的内容
        a = news.select('a')[0].attrs['href'] #链接
        '''print(data,title,description,a)'''
        detail_res = requests.get(a)
        detail_res.encoding = 'utf-8'
        detail_soup = BeautifulSoup(detail_res.text,'html.parser') #新闻详情页
        content = detail_soup.select('#content')[0].text #正文内容
        info = detail_soup.select('.show-info')[0].text #脚本信息
        data_time = info.lstrip('发布时间:')[:19]
        print(info)
    break

  

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

info = '发布时间:2018-03-30 10:19:23      作者:龙婷  审核:唐丽峰  来源:国际学院    摄影:龙婷    点击:次'
publish_time = info.lstrip('发布时间:')[:19]
sh = info[info.find('审核'):].split()[0].lstrip('审核:')
zz = info[info.find('作者:'):].split()[0].lstrip('作者:')
ly = info[info.find('来源:'):].split()[0].lstrip('来源:')
print(publish_time,sh,zz,ly)

  

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

#获取当前时间
now_time = datetime.now()
now_time.year
exchangetime = datetime.strptime(publish_time,"%Y-%m-%d %H:%M:%S") #字符转换为时间
exchangestring = now_time.strftime('%Y\%m\%d') #时间转换为字符
print(exchangetime)
print(exchangestring)

  

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

# -*- codding: UTF-8 -*-
# -*- author: WF -*-
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')

for news in soup.select('li'):
    if len(news.select('.news-list-title'))>0 : #排除为空的li
        data = news.select('.news-list-info')[0].contents[0].text #日期
        title = news.select('.news-list-title')[0].text #标题
        description = news.select('.news-list-description')[0].text #标题下的内容
        a = news.select('a')[0].attrs['href'] #链接
        print(data,title,description,a)
        detail_res = requests.get(a)
        detail_res.encoding = 'utf-8'
        detail_soup = BeautifulSoup(detail_res.text,'html.parser') #新闻详情页
        content = detail_soup.select('#content')[0].text #正文内容
        info = detail_soup.select('.show-info')[0].text #脚本信息
        data_time = info.lstrip('发布时间:')[:19]
        print(info)
    break

info = '发布时间:2018-03-30 10:19:23      作者:龙婷  审核:唐丽峰  来源:国际学院    摄影:龙婷    点击:次'
publish_time = info.lstrip('发布时间:')[:19]
sh = info[info.find('审核'):].split()[0].lstrip('审核:')
zz = info[info.find('作者:'):].split()[0].lstrip('作者:')
ly = info[info.find('来源:'):].split()[0].lstrip('来源:')
print(publish_time,sh,zz,ly)

#获取当前时间
now_time = datetime.now()
now_time.year
exchangetime = datetime.strptime(publish_time,"%Y-%m-%d %H:%M:%S") #字符转换为时间
exchangestring = now_time.strftime('%Y\%m\%d') #时间转换为字符
print(exchangetime)
print(exchangestring)

  

原文地址:https://www.cnblogs.com/qq2647409627/p/8710487.html