import requests import re from bs4 import BeautifulSoup from datetime import datetime newsurl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(newsurl) # 返回response對象 res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') def getNewDetail(newsUrl): for news in soup.select('li'): if len(news.select('.news-list-title'))>0: t=news.select('.news-list-title')[0].text #標題 a=news.select('a')[0].attrs['href'] #連接 res = requests.get(a) res.encoding = 'utf-8' soupd = BeautifulSoup(res.text, 'html.parser') content = soupd.select('#content')[0].text description = news.select('.news-list-description')[0].text resd=requests.get(a) resd.encoding='utf-8' soupd=BeautifulSoup(resd.text,'html.parser') info=soupd.select('.show-info')[0].text d=info.lstrip('發佈時間:')[:19] dt=datetime.strptime(d,'%Y-%m-%d %H:%M:%S') author=info[info.find('做者:'):].split()[0].lstrip('做者:') source=info[info.find('來源:'):].split()[0].lstrip('來源:') photo=info[info.find('攝影:'):].split()[0].lstrip('攝影:') print("新聞標題:",t) print("連接:",a) print("發佈時間:",dt) print("做者:",author) print("來源:",source) print("攝影:",photo) print("描述:",description) getClickCount(a) print("正文:", content) break def getClickCount(newsUrl): clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id=9183&modelid=80' count = requests.get(clickUrl).text.split('.html')[-1].lstrip("('").rstrip("');") print("點擊次數:", count) re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen(.*).html', newsUrl).group(1).split('/')[1] print('新聞編號:', re.search('\_(.*).html', newsUrl).group(1)) getNewDetail(newsurl)