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