您现在的位置是:首页 > 个人博客日记 个人博客日记

yiiCaina 站点自动签到程序 (python)

2019-06-28 17:12:24

yiiCaina 站点自动签到程序 (python)

https://www.yiichina.com/

  1. # coding=utf-8
  2. import requests
  3. import smtplib
  4. import json
  5. from pyquery import PyQuery as pq
  6. from email.mime.text import MIMEText
  7. from email.header import Header
  8. #安装插件
  9. # pip3.7 install requests
  10. # pip3.7 install PyQuery
  11. # 登录地址
  12. login_url = "https://www.yiichina.com/login"
  13. # 签到地址
  14. url2 = "https://www.yiichina.com/registration"
  15. # 构造登陆的header头
  16. headers = {
  17. 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
  18. 'Accept-Encoding': "gzip, deflate, br",
  19. 'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
  20. 'Cache-Control': "no-cache",
  21. 'Connection': "keep-alive",
  22. 'Content-Length': "250",
  23. 'Content-Type': "application/x-www-form-urlencoded",
  24. 'Host': "www.yiichina.com",
  25. 'Origin': "https://www.yiichina.com",
  26. 'Pragma': "no-cache",
  27. 'Referer': "https://www.yiichina.com/login",
  28. 'Upgrade-Insecure-Requests': "1",
  29. 'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
  30. "Chrome/67.0.3396.99 Safari/537.36 "
  31. }
  32. # 构造签到的header头
  33. headers2 = {
  34. 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
  35. 'Accept-Encoding': "gzip, deflate, br",
  36. 'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
  37. 'Cache-Control': "no-cache",
  38. 'Connection': "keep-alive",
  39. 'Content-Length': "250",
  40. 'Content-Type': "application/x-www-form-urlencoded",
  41. 'Host': "www.yiichina.com",
  42. 'Origin': "https://www.yiichina.com",
  43. 'Pragma': "no-cache",
  44. 'Referer': "https://www.yiichina.com/",
  45. 'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
  46. "Chrome/67.0.3396.99 Safari/537.36",
  47. 'X-CSRF-Token': "jiZGgvngdp0HiiclI4SheJlJr5-lVyymIFgbfE1ZUw33FWv2mI5DzyrJFUFHt8tO_h3az84GH9xEHEtFCh8cfg==",
  48. 'X-Requested-With': "XMLHttpRequest"
  49. }
  50. # 登陆方法和签到
  51. def login_sign():
  52. r_session = requests.Session()
  53. # 请求页面
  54. page = r_session.get(login_url)
  55. # 请求获得的内容
  56. content = pq(page.content)
  57. # 获取csrf_token
  58. item1 = content('meta[name=csrf-token]').items()
  59. csrf = ''
  60. for nurl in item1:
  61. csrf = nurl.attr('content')
  62. data = {
  63. "_csrf": csrf,
  64. "LoginForm[username]": "**********",
  65. "LoginForm[password]": "**********",
  66. 'LoginForm[rememberMe]': "0",
  67. "LoginForm[rememberMe]": "1",
  68. "login - button": ""
  69. }
  70. # 完成用户登录
  71. response1 = r_session.post(login_url, data=data, headers=headers)
  72. # 请求获得的内容
  73. content = pq(response1.content)
  74. # 获取csrf_token
  75. item1 = content('meta[name=csrf-token]').items()
  76. csrf = ''
  77. for nurl in item1:
  78. csrf = nurl.attr('content')
  79. data2 = {
  80. "_csrf": csrf
  81. }
  82. # 模拟签到请求
  83. response2 = r_session.post(url2, data=data2, headers=headers2)
  84. data = response2.text
  85. if data is not '':
  86. data = json.loads(data, encoding='utf-8')
  87. if data['status'] == 0:
  88. print(data['message'])
  89. sendEmail('yii China 签到程序,结果:' + data['message'])
  90. else:
  91. print(data)
  92. sendEmail('yii China 签到程序,结果:' + data['message'])
  93. else:
  94. sendEmail('yii China 签到程序,请求无结果!')
  95. def sendEmail(content: object) -> object:
  96. receivers = ['879042886@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
  97. sender = 'send@majiameng.com'
  98. subject = 'Python 签到警告!' #标题
  99. # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
  100. message = MIMEText(content, 'plain', 'utf-8')
  101. message['From'] = Header(sender, 'utf-8') # 发送者
  102. message['To'] = Header(str(receivers), 'utf-8') # 接收者
  103. message['Subject'] = Header(subject, 'utf-8')
  104. try:
  105. # 一般我们配置的smtp服务器端口都是25,不过有的服务器或空间提供商把25端口给禁用了,
  106. # 比如阿里云就给禁用了,这个可以找相应的提供商确认一下。
  107. # 如果真是禁用了25端口,可以采用465端口,这个端口很多主流的邮件服务商像网易邮箱、QQ邮箱、阿里云邮箱也都支持,采用了465端口,
  108. # smtpObj = smtplib.SMTP('smtp.mxhichina.com')
  109. smtpObj = smtplib.SMTP_SSL('邮箱网关', 465)
  110. smtpObj.login('邮箱帐号', '邮箱密码')
  111. smtpObj.sendmail(sender, receivers, message.as_string())
  112. smtpObj.close()
  113. print("Success: 邮件发送成功")
  114. return True
  115. except smtplib.SMTPException as e:
  116. print("Error: 无法发送邮件" + str(e))
  117. return False
  118. if __name__ == "__main__":
  119. try:
  120. login_sign()
  121. except Exception as e:
  122. print(str(e))
  123. sendEmail('yii China 签到程序,异常:' + str(e))


关注TinyMeng博客,更多精彩分享,敬请期待!
 

站点信息