获取全部校园新闻
1.取出一个新闻列表页的全部新闻 包装成函数。
2.获取总的新闻篇数,算出新闻总页数。
3.获取全部新闻列表页的全部新闻详情。
4.找一个自己感兴趣的主题,进行数据爬取,并进行分词分析。不能与其它同学雷同。
import requests from bs4 import BeautifulSoup from datetime import datetime import re def getNewsId(url): newsId = re.findall(r'\_(.*).html', url)[0][-4:] clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId) clickRes = requests.get(clickUrl) clickCount = int(re.search("hits'\).html\('(.*)'\);", clickRes.text).group(1)) return clickCount def getNewsDetail(newsUrl): resd = requests.get(newsUrl) resd.encoding = 'utf-8' soupd = BeautifulSoup(resd.text, 'html.parser') content = soupd.select('#content')[0].text info = soupd.select('.show-info')[0].text count = getNewsId(newsUrl) date = re.search('(\d{4}.\d{2}.\d{2}\s\d{2}.\d{2}.\d{2})', info).group(1) if(info.find('作者:')>0): author = re.search('作者:((.{2,4}\s|.{2,4}、){1,3})', info).group(1) if(info.find('审核:')>0): check = re.search('审核:((.{2,4}\s){1,3})', info).group(1) if(info.find('来源:')>0): sources = re.search('来源:(.*)\s*摄|点', info).group(1) dateTime = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') print('发布时间:{0}\n作者:{1}\n审核:{2}\n来源:{3}\n点击次数:{4}'.format(dateTime, author, check, sources, count)) print(content) def getListPage(listUrl): res = requests.get(listUrl) res.encoding = 'utf-8' soup = BeautifulSoup(res.text, 'html.parser') for new in soup.select('li'): if len(new.select('.news-list-title')) > 0: title = new.select('.news-list-title')[0].text description = new.select('.news-list-description')[0].text newsUrl = new.select('a')[0]['href'] print('标题:{0}\n内容:{1}\n链接:{2}'.format(title, description, newsUrl)) getNewsDetail(newsUrl) break listUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' getListPage(listUrl) res = requests.get(listUrl) res.encoding = 'utf-8' soup = BeautifulSoup(res.text, 'html.parser') listCount = int(soup.select('.a1')[0].text.rstrip('条'))//10+1 for i in range(2,listCount): listUrl= 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i) getListPage(listUrl)
import requests, re, jieba from bs4 import BeautifulSoup from datetime import datetime def getNewsDetail(newsUrl): resd = requests.get(newsUrl) resd.encoding = 'gb2312' soupd = BeautifulSoup(resd.text, 'html.parser') content = soupd.select('#endText')[0].text info = soupd.select('.post_time_source')[0].text date = re.search('(\d{4}.\d{2}.\d{2}\s\d{2}.\d{2}.\d{2})', info).group(1) dateTime = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') sources = re.search('来源:\s*(.*)', info).group(1) keyWords = getKeyWords(content) print('发布时间:{0}\n来源:{1}'.format(dateTime, sources)) print('关键词:{}、{}、{}'.format(keyWords[0], keyWords[1], keyWords[2])) print(content) def getKeyWords(content): content = ''.join(re.findall('[\u4e00-\u9fa5]', content)) wordSet = set(jieba._lcut(content)) wordDict = {} for i in wordSet: wordDict[i] = content.count(i) deleteList, keyWords = [], [] for i in wordDict.keys(): if len(i) < 2: deleteList.append(i) for i in deleteList: del wordDict[i] dictList = list(wordDict.items()) dictList.sort(key=lambda item: item[1], reverse=True) for i in range(3): keyWords.append(dictList[i][0]) return keyWords def getListPage(listUrl): res = requests.get(listUrl) res.encoding = 'gbk' soup = BeautifulSoup(res.text, 'html.parser') for new in soup.select('#news-flow-content')[0].select('li'): newsUrl = new.select('a')[0]['href'] title = new.select('a')[0].text print('标题:{0}\n链接:{1}'.format(title, newsUrl)) getNewsDetail(newsUrl) break listUrl = 'http://tech.163.com/it/' getListPage(listUrl) for i in range(2, 20): listUrl = 'http://tech.163.com/special/it_2016_%02d/' % i getListPage(listUrl)