-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathallCollegeScoreLine.py
More file actions
executable file
·61 lines (54 loc) · 1.69 KB
/
allCollegeScoreLine.py
File metadata and controls
executable file
·61 lines (54 loc) · 1.69 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import re
import urllib.request
import urllib
#获取所有学校的网站
#http://data.api.gkcx.eol.cn/soudaxue/queryschool.html?messtype=jsonp&page=11
#base method
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0')
response = urllib.request.urlopen(req)
return response.read()
def getHtml(page):
data={}
data['messtype']='jsonp'
data['page']=page
url_values=urllib.parse.urlencode(data)
url="http://data.api.gkcx.eol.cn/soudaxue/queryschool.html?"
full_url=url+url_values
# print(full_url)
data=url_open(full_url).decode('utf-8')
return data
def saveDataFile(fileContent):
path="..\\datatext\\allCollege\\allCollege.txt"
with open(path,"a",encoding='utf-8') as f:
f.write(fileContent)
#正则过滤显示所有的学校名
def regxGetCollegeName(html):
patter=re.compile(r'"schoolname.+?"(\w.+?)"',re.S)
return patter.findall(html)
#遍历所有的表格获取大学数据
def getAllCollege():
j=0
k=0
for i in range(276):
html_data=getHtml(i+1)
if html_data:
saveCollegeName=regxGetCollegeName(html_data)
saveDataFile(html_data)
for name in saveCollegeName:
k+=1
print("保存学校:"+name+" 到allCollege.txt文件.")
j+=1
print("共保存学校"+str(j)+"所!")
#################################
'''
读文件
'''
def getDataSource(sDataPath):
with open(sDataPath,"r",encoding='utf-8') as f:
return f.read()
if __name__ == '__main__':
getAllCollege()
# html=getDataSource("D:\\yaoding\\biyesheji\\dataSource\\datatext\\allCollege\\allCollege.txt")
# print(regxGetCollegeName(html))