Problem: After python successfully logs in, it prompts that there is no login when signing in, that is, the cookie is missing
I captured the packet through Fidder and found that compared with the successful manual login, the package sent by Python lacked cookies. The code below is, but I found that using the opener method, python will save the cookie for subsequent access
import urllib
from http import cookiejar
import gzip
def getOpener(head):
cookie = cookiejar.CookieJar()
pre = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(pre)
header = []
for key, value in head.items():
elem = (key, value)
header.append(elem)
opener.addheaders = header
return opener, cookie
#伪装浏览器的头部
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 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
'Host':'account.oneplus.cn',
'X-Requested-With':'XMLHttpRequest',
'Origin': 'http://account.oneplus.cn',
}
url = 'http://account.oneplus.cn/onepluslogin'
opener, cookie = getOpener(header)
def ungzip(data):
try: # 尝试解压
print('正在解压.....')
data = gzip.decompress(data)
print('解压完毕!')
except:
print('未经压缩, 无需解压')
return data
#
#
email = r'***********'
password = '****'
postDict = {
'loginName': email,
'passWord': password,
'source': '2',
'remember': '0',
'channel': '2',
'verifyCode': ''
}
postData = urllib.parse.urlencode(postDict).encode()
op = opener.open(url, postData)
data = op.read()
data = ungzip(data)
print(data)
#--------签到--------------
register_url = 'http://www.oneplusbbs.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1'
re_op = opener.open(register_url)
data = op.read()
data = ungzip(data)
print(data)
The reason for missing cookies is this
After sending a login request to http://account.oneplus.cn/one...
The returned data is like this
{u'defaultData': None, u'errCode': u'11025', u'ret' : u'1', u'data': {u'jumpUrl': None, u'isCartMerge': u'false', u'bbscookie': u'http://www.oneplusbbs.com/set... ', u'times': u'0'}, u'page': None, u'errMsg': None}
The host did not resend the setsocookie request, bbscookie
The reason why the sign-in failed is that the sign-in is a post request, and the author used get
The following is my code after practice, for reference only
Try using the requests module, this API is very convenient.
requests
account.oneplus.cn and www.oneplus.cn should be cross-domain