# -*- coding: UTF-8 -*- 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‘) #print(soup) 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 a = news.select(‘a‘)[0].attrs[‘href‘] print("date = "+d,"text = "+t,"href = "+a)
2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。
resd = requests.get(a) resd.encoding = ‘utf-8‘ soupd = BeautifulSoup(resd.text,‘html.parser‘) c = soupd.select(‘#content‘)[0].text info = soupd.select(".show-info") print(info[0].text)
3. 将其中的发布时间由str转换成datetime类型。
还没做好。