forked from Dayunxi/getUnivUrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunivRank.py
31 lines (26 loc) · 896 Bytes
/
univRank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import requests, bs4
from bs4 import BeautifulSoup
def getHTMLText(url):
try:
r = requests.get(url, timeout=10)
r.raise_for_status()
r.encoding = 'utf-8'
return r.text
except:
return ""
def fillUnivList(ulist, html):
soup = BeautifulSoup(html, "html.parser")
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag):
tds = tr('td')
ulist.append([tds[0].string, tds[1].string, tds[2].string, tds[3].string])
def main():
uinfo = []
url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
html = getHTMLText(url)
fillUnivList(uinfo, html)
with open('univ2016.csv', 'wt') as file:
for item in uinfo:
file.write('{},{},{},{}\n'.format(item[0], item[1], item[2], item[3]))
if __name__ == '__main__':
main()