河南大学教务系统爬虫!

众所周知,我们河南大学的教务处是没有搜索功能的

如果我们想要查询一个消息的话就要花费好久

所以就利用py爬虫把信息爬到mysql数据库,接着用java写一个前端来提供搜索来展示我们的教务通知

附上数代码

爬虫代码!

import requests
import pymysql
from lxml import etree

my_result = {}
url_list=["16","15","/14","/13","/12","/11","/10","/9","/8","/7","/6","/5","/4","/3","/2","/1"]
connection = pymysql.connect(
    host='154.8.165.65',
    user='henu',
    password='**********',  #我服务器的密码,不好公布大家
    database='henu',
    port=3306
)
cursor = connection.cursor()


def joint_String(my_list):
    merged_list = []
    for i in range(0, len(my_list), 2):
        if i + 1 < len(my_list):
            merged_list.append(my_list[i] +'?'+ my_list[i + 1])
    return merged_list


def students_massage():
    resp = requests.get(url + plus1)
    resp.encoding = "Utf-8"
    return resp.text


def teachers_massage():
    resp = requests.get(url + plus2)
    resp.encoding = "Utf-8"
    return resp.text

for i in url_list:
    url="https://jwc.henu.edu.cn/"
    plus1="jwzl/xszl"+i+".htm"
    plus2="jwzl/jszl"+i+".htm"

    et = etree.HTML(students_massage())
    ost_list = et.xpath("//div[@id='middle']//span//text()")
    ost_url_list = et.xpath("//div[@id='middle']//a/@href")
    nst_list=ost_list[6:-2]
    nst_list=joint_String(nst_list)
    nst_url_list=ost_url_list[5:-2]

    nnst_url_list = []

    for item in nst_url_list:
        nnst_url_list.append("https://jwc.henu.edu.cn/"+item.replace("..",""))

        my_dict = {key: value for key, value in zip(nst_list,nnst_url_list)}
        my_result.update(my_dict)

cursor = connection.cursor()

for key, value in my_result.items():
    parts = key.split('?')
    name = parts[0]
    time = parts[1]
    sql = "INSERT INTO ma (name, time,url) VALUES (%s, %s, %s)"
    values = (name,time,value)
    cursor.execute(sql, values)
    print("已提交一行数据")
connection.commit()
connection.close()

可以执行文件

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值