2. 这次爬取的网址请搜索“阿凡题”(纯技术讨论)
“阿凡题”(纯技术讨论
)
3. 在该网址选择查院校,其他都是默认
4. 这次爬取的信息主要是下图红框的内容,在浏览器开发者中,点击XHR就可以发现这个接口,接口的内容都有我们需要的信息。
5. 先构建请求头,请求头直接复制过来了
# 构建请求头
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'contentType': 'application/x-www-form-urlencoded; charset=utf-8',
'Cookie': 'cfm-major=true',
'Host': 'gaokao.afanti100.com',
'media': 'PC',
'Referer': 'http://gaokao.afanti100.com/university.html',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
6. 接下来先请求这条url,通过format方法实现对url的拼接,以此达到翻页的效果,通过查看接口的内容,发现是json格式,大学的信息在data键中的university_lst中,因此我们需要取出这个键,其中university_lst是列表。
def get_index():
page = 1
while True:
if page > 188:
break
url = 'http://gaokao.afanti100.com/api/v1/universitie