高考已经结束了,相信绝大部分同窗都在放松本身了,毕竟压抑了这么久。如今虽然距离高考放榜还有一段时间,可能有一些同窗已经火烧眉毛地想知道本身考的怎样。所以,如今就来爬取高考网上的近几年高考分数线,看一下近几年分数线的变化趋势,从而内心面有个底,这样才可以更加放松的去嗨皮。web
使用的工具库mongodb
beautifulsoup数据库
mongodb多线程
echartsapp
1.整体思路echarts
在高考网上,能够查看各省的分数线,其中文理科都有2009-2017年的数据,因此能够直接爬取这些数据下来存到MongoDB中,而后再使用echarts进行绘图展现,从而能够更加直观的看到高考分数线的变化趋势函数
2.爬取数据工具
有两种方法能够达到这个目的url
1).经过拼接URL连接切换省份,能够得出连接的变化规律:只要替换省份的拼音上去就能够请求到:spa
http://www.gaokao.com/guangdong/fsx/
http://www.gaokao.com/shanghai/fsx/
推荐使用pypinyin模块——汉字拼音转换模块/工具。直接使用lazy_pinyin方法就能够获得各省的拼音。因为返回的是列表,因此还须要处理一下才能使用。
>>> from pypinyin import lazy_pinyin
>>> lazy_pinyin('北京')
['bei', 'jing']
2).经过获取地区导航中的各省连接,直接获得URL
获取各省份的连接:
1 # 获取省份及连接 2 pro_link = [] 3 def get_provice(url): 4 web_data = requests.get(url, headers=header) 5 soup = BeautifulSoup(web_data.content, 'lxml') 6 provice_link = soup.select('.area_box > a') 7 for link in provice_link: 8 href = link['href'] 9 provice = link.select('span')[0].text 10 data = { 11 'href': href, 12 'provice': provice 13 } 14 provice_href.insert_one(data)#存入数据库 15 pro_link.append(href)
接下来就能够开始爬取分数线了,经过审查元素(以下图),直接使用beautifulsoup来过滤内容
# 获取分数线 def get_score(url): web_data = requests.get(url, headers=header) soup = BeautifulSoup(web_data.content, 'lxml') # 获取省份信息 provice = soup.select('.col-nav span')[0].text[0:-5] # 获取文理科 categories = soup.select('h3.ft14') category_list = [] for item in categories: category_list.append(item.text.strip().replace(' ', ''))#替换空格 # 获取分数 tables = soup.select('h3 ~ table') for index, table in enumerate(tables): tr = table.find_all('tr', attrs={'class': re.compile('^c_\S*')})#使用正则匹配 for j in tr: td = j.select('td') score_list = [] for k in td: # 获取每一年的分数 if 'class' not in k.attrs: score = k.text.strip() score_list.append(score) # 获取分数线类别 elif 'class' in k.attrs: score_line = k.text.strip() score_data = { 'provice': provice.strip(),#省份 'category': category_list[index],#文理科分类 'score_line': score_line,#分数线类别 'score_list': score_list#分数列表 } score_detail.insert_one(score_data)#插入数据库
因为有30多个省份,因此这里使用多线程来爬取,能够提升爬取效率。
if __name__ == '__main__': header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0', 'Connection': 'keep - alive' } url = 'http://www.gaokao.com/guangdong/fsx/' get_provice(url) pool = Pool() pool.map(get_score, [i for i in pro_link])#使用多线程
使用多线程爬取的话,不用1分钟就能够爬完全部的数据了。看,多线程可牛逼了,叉会腰先
3.数据可视化
爬取数据只是第一步,接下来就要对数据进行处理展现了。从mongodb 中查找出数据,对数据进行清洗整理,因为我这里的pyecharts有点问题,因此使用echarts进行展现
直接经过mongodb的find函数,限制查找的内容
import pymongo import charts client = pymongo.MongoClient('localhost', 27017) gaokao = client['gaokao'] score_detail = gaokao['score_detail'] # 筛选分数线、省份、文理科 def get_score(line,pro,cate): score_list=[] for i in score_detail.find({"$and":[{"score_line":line},{"provice":pro},{'category': cate}]}): score_list = i['score_list'] score_list.remove('-')#去掉没有数据的栏目 score_list = list(map(int, score_list)) score_list.reverse() return score_list
2).定义相关数据
# 获取文理科分数 line = '一本' pro = '北京' cate_wen = '文科' cate_li = '理科' wen=[] li = [] wen=get_score(line,pro,cate_wen)#文科 li=get_score(line,pro,cate_li)#理科 # 定义年份 year = [2017,2016,2015,2014,2013,2012,2011,2010,2009] year.reverse()
series = [ { 'name': '文 科', 'data': wen, 'type': 'line' }, { 'name': '理科', 'data': li, 'type': 'line', 'color':'#ff0066' } ] options = { 'chart' : {'zoomType':'xy'}, 'title' : {'text': '{}省{}分数线'.format(pro,line)}, 'subtitle': {'text': 'Source: gaokao.com'}, 'xAxis' : {'categories': year}, 'yAxis' : {'title': {'text': 'score'}} } charts.plot(series, options=options,show='inline')
这样就能够获得下面的历年分数线趋势图了。固然,能够修改get_score的参数就能够的到其余省份的信息了
4.预测分数线
经过折线图,能够大概的预测2018年北京高考一本的分数线:文科在550-560分之间;理科在530-540分之间。固然,这只是预测的,若是有特殊状况的话,可能波动会比较大。另外,还能够经过拉格朗日插值法求出今年的分数线,这样比较准确,可是因为过程比较麻烦,因此这里只是目测而已。