如何自动使用会话?

时间:2014-02-24 09:59:14

标签: python python-3.x authorization

请通过脚本帮助处理授权。问题是无法自动插入get-request invbf_session_id。

import pprint
import requests
import re
import shelve

import bs4


def scanning_posts():
    print('------------------------enter begin--------------------')

    url = 'http://forum.saransk.ru/'
    html = requests.get(url)

    pprint.pprint(html.headers)
    rawCookie = html.headers['Set-Cookie']
    cookie = re.search(r"invbf_session_id=(.*?);", rawCookie).group(1)
    pprint.pprint(cookie)   # cookie != zzzzzzzzzzzzzzzzzzzzzz

    html = requests.get(url)
    soup = bs4.BeautifulSoup(html.text)
    loginForm = soup.find('form', {'id': 'login'})
    hiddenAuthKey = soup.find('input', {'name': 'auth_key'})['value']

    authData = {    
                'ips_username': 'xxxxxx',
                'ips_password': 'yyyyyy',
                'auth_key': hiddenAuthKey,
                'rememberMe': 1,
                'referer':  'http://forum.saransk.ru/'
    }

    header = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36',
                'Referer': 'http://forum.saransk.ru/forum/'
    }

    #pprint.pprint(authData)
    print('\tlogin: ', authData['ips_username'])

    cookie = dict(invbf_session_id='zzzzzzzzzzzzzzzzzzzzzz')



    req = requests.get(url, params=authData, cookies=cookie, headers=header)
    soup = bs4.BeautifulSoup(req.text)

    signLinkNotLogged = soup.find('a', {'id': 'sign_in'})
    if signLinkNotLogged:
        print('------------------------enter failed--------------------')
    else:
        print('------------------------enter successful--------------------')


scanning_posts()

运行脚本后显示错误的值invbf_session_id,如FF firebug中所示。分别没有获得授权。

如果值为invbf_session_id,则复制FF firebug并粘贴脚本,授权成功

0 个答案:

没有答案