搜尋

首頁  >  問答  >  主體

python網站登入並簽到問題

問題:python登入成功後,簽到時提示沒有登錄,也就是缺少cookie

#我透過Fidder抓包發現和手動登入成功相比,Python發出去的包缺少cookie下面是代碼,但是我查到用opener方式,python會保存cookie進行後繼的訪問

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)
天蓬老师天蓬老师2751 天前791

全部回覆(3)我來回復

  • PHPz

    PHPz2017-05-18 11:01:42

    缺少cookie原因是這樣的

    發了login請求後http://account.oneplus.cn/one...
    回傳資料:是這樣的
    {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}
    樓主沒有重發setsocookie請求,bbscookie

    還有簽到失敗的原因是,簽到是post請求,樓主用get了

    以下是我實踐後的程式碼,僅供參考

    import re
    import requests
    
    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'
    email = '邮箱'
    password = '密码'
    
    postDict = {    'loginName': email,    'passWord': password,    'source': '2',    'remember': '0',    'channel': '2',    'verifyCode': '' }
    
    session = requests.Session()
    
    r = session.post(url, data=postDict)
    data = r.json()
    print(data)
    r = session.get(data['data']['bbscookie'])
    
    sign_url = 'http://www.oneplusbbs.com/plugin.php?id=dsu_paulsign:sign'
    
    r = session.get(sign_url)
    m = re.search('name="formhash" value="([^"]+)"', r.text)
    
    formhash = m.group(1)
    
    print(formhash)
    
    qiandao_url = 'http://www.oneplusbbs.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1'
    
    form = {'formhash': formhash,
            'qdxq':'fd',
            'qdmode':1,
            'todaysay':'helloworld'
            }
    
    r = session.post(qiandao_url, data=form)
    
    print(r.text)
    

    回覆
    0
  • PHP中文网

    PHP中文网2017-05-18 11:01:42

    你使用 requests 模組試試,這個 API 很方便。
    requests

    回覆
    0
  • 阿神

    阿神2017-05-18 11:01:42

    account.oneplus.cn和www.oneplus.cn,應該是跨域了

    回覆
    0
  • 取消回覆