1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。
2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。
3. 将其中的发布时间由str转换成datetime类型。
4. 将完整的代码及运行结果截图发布在作业上。
#-*- coding: UTF-8 -*- 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: print(news.select('.news-list-title')) for news in soup.select('li'): if len(news.select('.news-list-title')) > 0: title = news.select('.news-list-title')[0].text dt = news.select('.news-list-info')[0].contents[0].text a = news.select('a')[0].attrs['href'] print(title, dt, a) for news in soup.select('li'): if len(news.select(".news-list-title")) > 0: title = news.select('.news-list-title')[0].text dt = news.select('.news-list-info')[0].contents[0].text a = news.select('a')[0].attrs['href'] print(title,dt,a) for news in soup.select("li"): if len(news.select(".news-list-title")) > 0: a = news.select('a')[0].attrs['href']#新闻链接 title = news.select('.news-list-title')[0].text#标题 res1 = requests.get(a) res1.encoding = 'utf-8' soup1 = BeautifulSoup(res1.text,'html.parser') dt = soup1.select('.show-info')[0].text#时间 print(title,' ',dt,' ') print(soup1.select('#content')[0].text)#内容 t1 = dt.lstrip('发布时间:')[:19] i = dt.find('来源:') if i>0: s=dt[dt.find('来源:'):].split()[0].lstrip('来源:') a = dt[dt.find('作者:'):].split()[0].lstrip('作者:') p = dt[dt.find('摄影:'):].split()[0].lstrip('摄影:') print(t1,a,s,p) break str = '2018-03-30 17:10:12 ' datetime.strptime(str,'%Y-%m-%d %H:%M:%S ') print(' ',str)