吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1714|回复: 7
收起左侧

[Python 转载] 【python】2020高考院校库下载保存excel

[复制链接]
howyouxiu 发表于 2020-7-27 12:38
本帖最后由 howyouxiu 于 2020-7-27 20:49 编辑

声明:文章转自@我是小人物,如有违规请删帖



成果展示(篇幅原因,展示部分,全国共两千多所高校):
image.png
这是原网页数据:
image.png

思路:查看网页源码发现为固定数据,非异步请求,所以呢就直接构造连接了
image.png 通过对比发现需要构造处就是红框部分,依次增加20使用xpath获取表格类数据比较方便源码:
[Python] 纯文本查看 复制代码
import requests
from lxml import etree
import openpyxl

title = ['院校名称', '院校所在地', '教育主管部门', '院校类型', '学历层次', '满意度']
workbook = openpyxl.Workbook()
sheet = workbook.worksheets[0]
sheet.append(title)


def writefile(school, destination, party, schooltype, floattype, score):
    for i in range(len(school)):
        sheet.append([school[i], destination[i], party[i], schooltype[i], floattype[i], score[i]])


def replacet(who):
    for i in range(len(who)):
        who[i] = who[i].replace(' ', '').replace('\n', '')
    return who


def get(url):
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                             "Chrome/78.0.3904.97 Safari/537.36", }
    response = requests.get(url, headers=headers).text

    html = etree.HTML(response, etree.HTMLParser())
    school = html.xpath('//div/table/tr/td[1]/a/text()')
    destination = html.xpath('//div/table/tr/td[2]/text()')
    party = html.xpath('//div/table/tr/td[3]/text()')
    schooltype = html.xpath('//div/table/tr/td[4]/text()')
    floattype = html.xpath('//div/table/tr/td[5]/text()')
    score = html.xpath('//div/table/tr/td[9]/a/text()')

    school = replacet(school)
    destination = replacet(destination)
    party = replacet(party)
    schooltype = replacet(schooltype)
    floattype = replacet(floattype)
    score = replacet(score)

    writefile(school, destination, party, schooltype, floattype, score)


if __name__ == '__main__':
    for p in range(0, 2820, 20):
        print('第{}个开始'.format(p))
        try:
            get('https://gaokao.chsi.com.cn/sch/search--ss-on,searchType-1,option-qg,start-{}.dhtml'.format(p))
            print('第{}个保存完成'.format(p))
        except:
            print('第{}个保存失败'.format(p))
    workbook.save('2020高考高校信息库.xlsx')
    workbook.close()

完成!源码及excel下载地址:https://haogeshare.lanzouj.com/iixmYf0zruh

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
biiadw + 1 + 1 我很赞同!
fanvalen + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| howyouxiu 发表于 2020-8-4 11:26
biiadw 发表于 2020-8-4 10:45
楼主,密码是什么呢?

忘了,忘了,密码是:52pj
苍生十年劫 发表于 2020-7-28 10:46
jajazhou 发表于 2020-7-28 11:17
niu645509965 发表于 2020-8-3 22:20
赞一个,转给有需要的人
shangjin1977 发表于 2020-8-4 09:25
能不能爬个历年专业录取分数
 楼主| howyouxiu 发表于 2020-8-4 10:38
shangjin1977 发表于 2020-8-4 09:25
能不能爬个历年专业录取分数

这个工作量有点大,就得存数据库了,可以搜索一下有没有相关的
biiadw 发表于 2020-8-4 10:45
楼主,密码是什么呢?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-18 12:13

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表