学完python不久觉得可以写一个自动签到的东西来省点力气,而且也可以赚取积分。
一开始的文章是看的这个:http://blog.csdn.net/u283056051/article/details/49946981
上面分析的很清晰,我就不再多说了。不过该博文的代码有些许错误(我猜应该是网站有更改)
就是关于令牌(token)的问题。
打开fiddle,登录后点开网页对应的request的webforms,发现在email,password下面还有个叫token的东西(请自行百度)
那么我们在传递postdata的时候就必须加上token,不然网站就会知道并不是用户在操作,而是机器人,于是拒绝访问。
解决方案很简单,在postdata里加上token就好了:
id = "xxx"
ps = "xxxx"
post = {
'email':id,
'password':ps,
'token':xxx',
}//emailm,password,token的值都可以直接从fiddle里面复制下来,
postData = urllib.parse.urlencode(post).encode()
完整代码:
import urllib
import urllib.request
import gzip
import http.cookiejar
import time
def getOpener(head):
cj = http.cookiejar.CookieJar()
pro = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(pro)
header = []
for key,value in head.items():
elem = (key,value)
header.append(elem)
opener.addheaders = header
return opener
def ungzip(data):
try:
print("正在解压")
data = gzip.decompress(data)
print("解压完毕")
except :
print("未经压缩")
return data
header = {
'Connection': 'Keep-Alive',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Accept-Encoding': 'gzip, deflate',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.17sucai.com',
}
url = "http://www.17sucai.com/auth"
opener = getOpener(header)
id = "
[email protected]"
ps = "7c4a8d09ca3762af61e59520943dc26494f8941b"
post = {
'email':id,
'password':ps,
'token':'cb3EAr1DiRNNtxDAKhBrMhE0hql1xauYBpdNmVhtKY4=',
}
postData = urllib.parse.urlencode(post).encode()
op = opener.open(url,postData)
data = op.read()
data = ungzip(data)
print(data)
url = 'http://www.17sucai.com/member/signin'
op = opener.open(url)
data = op.read()
data = ungzip(data)
print(data)
输出结果是b'{"code":200,"day":1,"score":20}'
code的值为200,并且有score,说明你签到成功了