前言
國(guó)內(nèi)大學(xué)最新排名,北大反超,浙大僅第四,中科大跌至第八
時(shí)隔五年,“雙一流”大學(xué)即將迎來首次大考,這也是繼改變高校評(píng)斷標(biāo)準(zhǔn)之后,第一次即將以官方對(duì)外發(fā)布,自然是引來了許多人的關(guān)注。最近,有許多不同機(jī)構(gòu)發(fā)布的國(guó)內(nèi)高校排名,但彼此之間的差異很大,網(wǎng)友之間的爭(zhēng)議也很大。
私信小編01即可獲取大量Python/ target=_blank class=infotextkey>Python學(xué)習(xí)資料
項(xiàng)目目標(biāo)
爬取高三網(wǎng)大學(xué)排名,并保存
目標(biāo)網(wǎng)址
http://m.gaosan.com/gaokao/265440.html
基本環(huán)境配置
- python 3.6 pycharm
爬蟲代碼
導(dǎo)入工具
import requests
import parsel
import csv
請(qǐng)求網(wǎng)頁數(shù)據(jù)
?
url = 'http://m.gaosan.com/gaokao/265440.html'
headers = {
'User-Agent': 'Mozilla/5.0 (windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
response.encoding = response.apparent_encoding
爬取數(shù)據(jù)
selector = parsel.Selector(response.text)
trs = selector.css('#page tr')
for tr in trs:
dit = {}
ranking = tr.css('td:nth-child(1)::text').get()
dit['名次'] = ranking
school = tr.css('td:nth-child(2)::text').get()
dit['學(xué)校名稱'] = school
score = tr.css('td:nth-child(3)::text').get()
dit['綜合得分'] = score
star = tr.css('td:nth-child(4)::text').get()
dit['星級(jí)排名'] = star
level = tr.css('td:nth-child(5)::text').get()
dit['辦學(xué)層次'] = level
csv_writer.writerow(dit)
?
保存數(shù)據(jù)
f = open('排名.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['名次', '學(xué)校名稱', '綜合得分', '星級(jí)排名', '辦學(xué)層次'])
f.close()
運(yùn)行代碼,效果如下圖
?
?
?






