高考已經結束了,相信絕大部分同窗都在放鬆本身了,畢竟壓抑了這麼久。如今雖然距離高考放榜還有一段時間,可能有一些同窗已經火燒眉毛地想知道本身考的怎樣。所以,如今就來爬取高考網上的近幾年高考分數線,看一下近幾年分數線的變化趨勢,從而內心面有個底,這樣纔可以更加放鬆的去嗨皮。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分之間。固然,這只是預測的,若是有特殊狀況的話,可能波動會比較大。另外,還能夠經過拉格朗日插值法求出今年的分數線,這樣比較準確,可是因爲過程比較麻煩,因此這裏只是目測而已。