校园新闻内容提取。
import requests from bs4 import BeautifulSoup 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: title=news.select('.news-list-title')[0].text #标题 url=news.select('a')[0]['href']#网址 time=news.select('.news-list-info')[0].contents[0].text#时间 bumen=news.select('.news-list-info')[0].contents[1].text#院系 txt=news.select('.news-list-description')[0].text #正文 print(time,title,bumen,txt,url)