起因
注册了一个网站,每天登陆签到会有奖励,但是打开浏览器,输入账号登陆再去点击签到觉得很麻烦。最近又接触了python,所以想着用python来实现一键签到。
首先放上整体代码
import requests
from lxml import etree
url = 'https://xxx/auth/login'
data = {"email":"xxxx", "passwd":"xxxxx", "code":""}
# 建立会话
s = requests.Session()
login_html = s.post(url, data)
qiandao = s.post("https://xxx/user/checkin")
user_html = s.get("https://xxx/user")
html = etree.HTML(user_html.text)
# 拿到剩余流量
ll = html.xpath("//code[@id='remain']")
s.close()
print("剩余流量:", ll[0].text)
print(qiandao.text)
简单介绍
得益于python丰富的库
可以对网页进行访问操作
导入使用到的库
import requests
from lxml import etree
首先对网站进行分析,我这个网站登录就是post提交表单,没有验证码等验证方式,所以很简单。当然也没有反爬虫机制。
url = 'https://xxx/auth/login'
data = {"email":"xxxx", "passwd":"xxxxx", "code":""}
接下来就是post提交数据进行登录,这时候需要保存会话,后面的签到操作需要在登录的状态下进行(废话)。
# 建立会话
s = requests.Session()
login_html = s.post(url, data)
qiandao = s.post("https://xxx/user/checkin")
再找到网页的签到的请求地址路径 注意请求的方式,到这里就已经完成了,下面的就是获取签到后的页面拿到我想要的数据,再将签到返回的结果输出。
user_html = s.get("https://xxx/user")
html = etree.HTML(user_html.text)
# 拿到剩余流量
ll = html.xpath("//code[@id='remain']")
s.close()
print("剩余流量:", ll[0].text)
print(qiandao.text)