软考的成绩公布一般不知道何时,只能了解大概的时间,对于强迫症的我通常是一遍一遍的刷着网页,看已经发布了没有。想了想我还是写一个爬虫自动通知吧。

爬虫Demo

  1. from bs4 import BeautifulSoup
  2. import requests
  3. import gzip
  4. import json
  5. import time
  6. import random
  7. import datetime
  8. from sample import Sample
  9. import sys
  10. def last_exam():
  11. payload = {}
  12. headers = {
  13. 'Referer': 'https://www.ruankao.org.cn/',
  14. 'Cookie': 'acw_tc=2f6a1fa216394476674883497e22bcb8a599b68e914cb69d6438d82b1ccf6e; PHPSESSID=if3qrt973hq8b104k8sumijkg3; SERVERID=f7154803a54565d8f743b36388c92cfa|1639447880|1639447667'
  15. }
  16. url = "https://query.ruankao.org.cn/score/main"
  17. response = requests.request("GET", url, headers=headers, data=payload)
  18. html = response.text
  19. soup = BeautifulSoup(html, 'html.parser')
  20. # print(soup.prettify())
  21. last_exam = soup.find_all(class_='select')[0].find_all('li')[-1]['data-value']
  22. # print(last_exam)
  23. return last_exam
  24. def is_pub():
  25. ret = False
  26. new_exam = last_exam()
  27. print('{},最新公布成绩:{}'.format(datetime.datetime.now(), new_exam))
  28. if new_exam == '2021年下半年':
  29. print('考试成绩已经公布')
  30. Sample.main(sys.argv[1:])
  31. print('已发送短信通知!')
  32. ret = True
  33. # else:
  34. # print('2021年下半年考试成绩未公布!')
  35. return ret
  36. if __name__ == '__main__':
  37. while True:
  38. if is_pub() is not True:
  39. time.sleep(600 + 600 * random.random())
  40. else:
  41. break

通知方式

想了想通过微信、邮件和短信的方式。邮件我一般不怎么看,微信通知还不太会,最终选择了短信通知的方式。选择了一下百度短信和阿里云短息,都需要提交企业信息,我现在没有。阿里云的短息包更便宜,最终选择了阿里云的短信包,经测试能发送测试短息,对于我来说这就够了。

示例代码,大概这个样子

  1. client = Sample.create_client('ACCESS_KEY_ID', 'ACCESS_KEY_SECRET')
  2. send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
  3. sign_name='阿里云短信测试',
  4. template_code='SMS_154950909',
  5. phone_numbers='18948345257',
  6. template_param='{"code":"6666"}'
  7. )
  8. resp = client.send_sms(send_sms_request)
  9. ConsoleClient.log(UtilClient.to_jsonstring(TeaCore.to_map(resp)))

最后

image.png
现在就坐等收到短信通知啦!

初稿
2021-12-14