import requests from datetime import datetime 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 s in (soup.select(‘li‘)): if(len(s.select(‘.news-list-title‘))>0): title=(s.select(‘.news-list-title‘)[0].text)#标题 d=(s.select(‘.news-list-description‘)[0].text) a=s.a.attrs[‘href‘]#链接 resd=requests.get(a) resd.encoding=‘utf-8‘ soupd = BeautifulSoup(resd.text, ‘html.parser‘) timet = soupd.select(‘.show-info‘) c = soupd.select(‘#content‘)[0].text#正文 e= soupd.select(‘.show-info‘)[0].text message=e.split() time=message[0]+‘ ‘+ message[1]#时间 author=message[2]#作者 safer=message[2]#审核 come=message[3]#来源 pho=message[4]#摄影 click=message[5]#点击 print(‘标题‘+ ‘: ‘+ title ) print(‘链接‘ + ‘: ‘ + a) print(‘正文‘ + ‘ :‘ + c) print(time) print(author) print(safer) print(come) print(pho) print(click) break
截图: