[关闭]
@hx 2018-04-25T16:42:38.000000Z 字数 1496 阅读 1570

使用requests和BeautifulSoup爬取教务系统个人信息

Python


  1. import requests
  2. from bs4 import BeautifulSoup
  3. if __name__ == "__main__":
  4. def getData(myInfo):
  5. # 首页
  6. homeUrl = "http://uia.whxy.edu.cn/cas/login?service=http%3A%2F%2Fportal.whxy.edu.cn%2Fc%2Fportal%2Flogin"
  7. # 登录
  8. loginUrl = homeUrl + "%3Fredirect%3D%252Fweb%252Fguest%252Findex%26p_l_id%3D10213"
  9. # 教务登录
  10. jwLogin = "http://59.172.226.5/eams/sso/login.action"
  11. # 教务主页
  12. jwHome = "http://59.172.226.5/eams/home.action"
  13. # 学籍信息
  14. xj = "http://59.172.226.5/eams/stdDetail.action"
  15. # 课表
  16. kb = "http://59.172.226.5/eams/courseTableForStd.action"
  17. kb2 = "http://59.172.226.5/eams/courseTableForStd!courseTable.action"
  18. s = requests.Session()
  19. r = s.get(homeUrl)
  20. r.encoding = r.apparent_encoding
  21. soup = BeautifulSoup(r.text, "html.parser")
  22. lt = soup.find(attrs={"name": "lt"})["value"]
  23. # 获取 lt
  24. myInfo["lt"] = lt
  25. myInfo["_eventId"] = "submit"
  26. r = s.post(loginUrl, data=myInfo)
  27. soup = BeautifulSoup(r.text, "html.parser")
  28. # 一卡通余额
  29. balance = soup.select_one("#balance").string
  30. r = s.get(jwLogin)
  31. r = s.get(jwHome)
  32. r = s.get(xj)
  33. soup = BeautifulSoup(r.text, "html.parser")
  34. data_list = {"一卡通余额": balance}
  35. for idx, tr in enumerate(soup.find_all('tr')):
  36. for tdx, td in enumerate(tr.find_all("td")):
  37. if len(td.contents) != 0:
  38. if idx == 1 and tdx == 1:
  39. data_list["学号"] = td.contents[0]
  40. elif idx == 1 and tdx == 3:
  41. data_list["姓名"] = td.contents[0]
  42. elif idx == 2 and tdx == 3:
  43. data_list["性别"] = td.contents[0]
  44. elif idx == 5 and tdx == 3:
  45. data_list["学院"] = td.contents[0]
  46. elif idx == 6 and tdx == 1:
  47. data_list["专业"] = td.contents[0]
  48. elif idx == 11 and tdx == 3:
  49. data_list["班级"] = td.contents[0]
  50. print(data_list)
  51. info = {"username": "2015****", "password": "Whxy27****"}
  52. getData(info)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注