今天你教高考生一個裝逼神技!利用Python爬取曆年高考成績!

來源:互聯網
上載者:User

標籤:url   plot   series   strip   sts   god   int   requests   今天   

2.爬取資料1.擷取各省的分數線資訊擷取各省份的連結:# 擷取分數線 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)#插入資料庫 3.資料視覺效果爬取資料只是第一步,接下來就要對資料進行處理展示了。從mongodb 中尋找出資料,對資料進行清洗整理,由於我這裡的pyecharts有點問題,所以使用echarts進行展示1).篩選省份等資訊直接通過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() 3).折線圖展示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‘)私信小編01即可擷取源碼和神秘大禮包哦!

今天你教高考生一個裝逼神技!利用Python爬取曆年高考成績!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.