爬取全国高校

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
#!/usr/bin/python
# coding: UTF-8
#爬取各省高校名单
import requests
import re

url=r'http://college.gaokao.com/schlist/a1/';
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6726.400 QQBrowser/10.2.2265.400'
}
html=requests.get(url,headers=headers)
#print(html.text)
#text='<a class="on" href="http://college.gaokao.com/schlist/a1/">北京</a>'
result=re.findall('href="(http://college.gaokao.com/schlist/[\S]+/)">([\S]+)</a>',html.text)[0:34]

all=[]
allregion=[]
n=0
for url,region in result:
html=requests.get(url,headers=headers)
r=re.findall('1/([\d]+)页',html.text)
if(r):
a=[]
allregion.append(region)
print(region+':'+r[0])
for id in range(1,int(r[0])+1):
html_school=requests.get(url+'p%d/'%(id),headers=headers)
#text='<a href="http://college.gaokao.com/school/211/" target="_blank">东北电力大学</a>'
res_shool=re.findall('<strong title="(\S+)" class="blue">',html_school.text)
a+=res_shool
print(a)
all.append(a)
n=len(allregion)
#print(result)