Exemplo n.º 1
0
def build_opener_with_cookies(domain=None):
    #读入cookies
    cookiejar = http.cookiejar.MozillaCookieJar()  # No cookies stored yet
    arr = getChromeCookies(domain)

    for a in arr:
        cookie_item = http.cookiejar.Cookie(
            version=0,
            name=str(a['name']),
            value=str(a['value']),
            port=None,
            port_specified=None,
            domain=str(a['hostname']),
            domain_specified=None,
            domain_initial_dot=None,
            path=str(a['path']),
            path_specified=None,
            secure=None,
            expires=None,
            discard=None,
            comment=None,
            comment_url=None,
            rest=None,
            rfc2109=False,
        )
        cookiejar.set_cookie(
            cookie_item)  # Apply each cookie_item to cookiejar
    ckproc = urllib.request.HTTPCookieProcessor(cookiejar)
    opener = urllib.request.build_opener(ckproc)
    return opener
Exemplo n.º 2
0
def main(request):
    opener = get_opener(header)

    opener.open(url)
    # get cookies
    value = request.COOKIES["scgysumis"]
    cookiejar = opener.handlers[-2].cookiejar
    cookie = []
    for i in cookiejar:
        cookie.append(i)
    cookie = cookie[0]

    cookie.value = value
    cookiejar.clear()
    cookiejar.set_cookie(cookie)

    data = data_input(request)

    login_page = login(opener, data).read().decode("gb2312")

    para = query_lesson(opener, data)
    if para == None:
        return HttpResponse('<script>alert("未找到该课程");location.assign("http://wechat.ustc.edu.cn/mis");</script>')

    return loop(opener, para)
Exemplo n.º 3
0
def get_chrome_cookies(domain=None):
    cookie_file_path = os.path.join(os.environ['LOCALAPPDATA'],r'Google\Chrome\User Data\Default\Cookies')
    print('Cookies文件的地址为:%s' % cookie_file_path)
    if not os.path.exists(cookie_file_path):
        raise Exception('Cookies 文件不存在...')
    coon = sqlite3.connect(cookie_file_path)
    sql = 'select host_key,name,value,path,encrypted_value from cookies'
    if domain:
        sql += ' where host_key like "%{}%"'.format(domain)
    cookiejar = http.cookiejar.CookieJar()
    for row in coon.execute(sql):
        ret = win32crypt.CryptUnprotectData(row[4], None, None, None, 0)
        cookie_item = http.cookiejar.Cookie(
            version=0,
            name=row[1],
            value=ret[1].decode(),
            port=None,
            port_specified=None,
            domain=row[0],
            domain_specified=None,
            domain_initial_dot=None,
            path=row[3],
            path_specified=None,
            secure=None,
            expires=None,
            discard=None,
            comment=None,
            comment_url=None,
            rest=None,
            rfc2109=False
        )
        cookiejar.set_cookie(cookie_item)
    coon.close()
    return urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar))
def get_chrome_cookies(domain=None):
    cookie_file_path = os.path.join(
        os.environ['LOCALAPPDATA'], r'Google\Chrome\User Data\Default\Cookies')
    print('Cookies文件的地址为:%s' % cookie_file_path)
    if not os.path.exists(cookie_file_path):
        raise Exception('Cookies 文件不存在...')
    coon = sqlite3.connect(cookie_file_path)
    sql = 'select host_key,name,value,path,encrypted_value from cookies'
    if domain:
        sql += ' where host_key like "%{}%"'.format(domain)
    cookiejar = http.cookiejar.CookieJar()
    for row in coon.execute(sql):
        ret = win32crypt.CryptUnprotectData(row[4], None, None, None, 0)
        cookie_item = http.cookiejar.Cookie(version=0,
                                            name=row[1],
                                            value=ret[1].decode(),
                                            port=None,
                                            port_specified=None,
                                            domain=row[0],
                                            domain_specified=None,
                                            domain_initial_dot=None,
                                            path=row[3],
                                            path_specified=None,
                                            secure=None,
                                            expires=None,
                                            discard=None,
                                            comment=None,
                                            comment_url=None,
                                            rest=None,
                                            rfc2109=False)
        cookiejar.set_cookie(cookie_item)
    coon.close()
    return urllib.request.build_opener(
        urllib.request.HTTPCookieProcessor(cookiejar))
Exemplo n.º 5
0
def build_opener_with_cookies(domain=None):
    #读入cookies
    s=''
    try:
        f=open(datapath+'/'+domain+'-cookiesAccess.txt','r');
        s=f.read()
        f.close()
    except:
        f=open(datapath+'/'+domain+'-cookiesAccess.txt','w');
        f.close();
        print (datapath+'/'+domain+'-cookiesAccess.txt is empty!')

    cookiejar = http.cookiejar.MozillaCookieJar()    # No cookies stored yet
    if(s!=''):
        arr=s.split(';')
        for i in arr:
            if i!='':
                a=i.split('=')
                cookie_item = http.cookiejar.Cookie(
                    version=0, name=a[0], value=a[1],
                             port=None, port_specified=None,
                             domain=domain, domain_specified=None, domain_initial_dot=None,
                             path=r'/', path_specified=None,
                             secure=None,
                             expires=None,
                             discard=None,
                             comment=None,
                             comment_url=None,
                             rest=None,
                             rfc2109=False,
                    )
                cookiejar.set_cookie(cookie_item)    # Apply each cookie_item to cookiejar
    ckproc=urllib.request.HTTPCookieProcessor(cookiejar)
    opener=urllib.request.build_opener(ckproc)
    return opener
Exemplo n.º 6
0
 def _store_cookies(self):
     """Store the session's cookiejar in a cookies.txt file"""
     if self._cookiefile and self.config("cookies-update", True):
         cookiejar = http.cookiejar.MozillaCookieJar()
         for cookie in self._cookiejar:
             cookiejar.set_cookie(cookie)
         try:
             cookiejar.save(self._cookiefile)
         except OSError as exc:
             self.log.warning("cookies: %s", exc)
Exemplo n.º 7
0
def save_cookies(cookies, filename):
    cookiejar = http.cookiejar.MozillaCookieJar(filename)
    for c in cookies:
        args = dict(vars(c).items())
        args['rest'] = args['_rest']
        del args['_rest']

        c = http.cookiejar.Cookie(**args)
        cookiejar.set_cookie(c)
    cookiejar.save(filename)
Exemplo n.º 8
0
def build_opener_with_chrome_cookies(domain=None):
    cookie_file_path = os.path.join(
        os.environ['LOCALAPPDATA'], r'Google\Chrome\User Data\Default\Cookies')
    if not os.path.exists(cookie_file_path):
        raise Exception('Cookies file not exist!')
    conn = sqlite3.connect(cookie_file_path)
    # sql = 'select host_key, name, value, path from cookies'
    sql = "select host_key,name,encrypted_value,path from cookies"
    if domain:
        sql += ' where host_key like "%{}%"'.format(domain)

    cj = cookiejar.CookieJar()  # No cookies stored yet

    for row in conn.execute(sql):
        pwdHash = str(row[2])
        try:
            ret = win32crypt.CryptUnprotectData(pwdHash, None, None, None, 0)
        except:
            print('Fail to decrypt chrome cookies')
            sys.exit(-1)

        cookie_item = cj.Cookie(
            version=0,
            name=row[1],
            value=ret[1],
            port=None,
            port_specified=None,
            domain=row[0],
            domain_specified=None,
            domain_initial_dot=None,
            path=row[3],
            path_specified=None,
            secure=None,
            expires=None,
            discard=None,
            comment=None,
            comment_url=None,
            rest=None,
            rfc2109=False,
        )
        cookiejar.set_cookie(
            cookie_item)  # Apply each cookie_item to cookiejar
    conn.close()

    proxy = {'http': '27.24.163.155:10'}
    return urllib.request.build_opener(
        urllib.request.ProxyHandler(proxy),
        urllib.request.HTTPCookieProcessor(cookiejar))
Exemplo n.º 9
0
def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):
    """Returns a CookieJar from a key/value dictionary.

    :param cookie_dict: Dict of key/values to insert into CookieJar.
    :param cookiejar: (optional) A cookiejar to add the cookies to.
    :param overwrite: (optional) If False, will not replace cookies
        already in the jar with new ones.
    :rtype: CookieJar
    """
    if cookiejar is None:
        cookiejar = RequestsCookieJar()

    if cookie_dict is not None:
        names_from_jar = [cookie.name for cookie in cookiejar]
        for name in cookie_dict:
            if overwrite or (name not in names_from_jar):
                cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))

    return cookiejar
Exemplo n.º 10
0
def build_opener_with_cookies(domain=None):
    #读入cookies
    s = ''
    try:
        f = open(datapath + '/' + domain + '-cookiesAccess.txt', 'r')
        s = f.read()
        f.close()
    except:
        f = open(datapath + '/' + domain + '-cookiesAccess.txt', 'w')
        f.close()
        print(datapath + '/' + domain + '-cookiesAccess.txt is empty!')

    cookiejar = http.cookiejar.MozillaCookieJar()  # No cookies stored yet
    if (s != ''):
        arr = s.split(';')
        for i in arr:
            if i != '':
                a = i.split('=')
                cookie_item = http.cookiejar.Cookie(
                    version=0,
                    name=a[0],
                    value=a[1],
                    port=None,
                    port_specified=None,
                    domain=domain,
                    domain_specified=None,
                    domain_initial_dot=None,
                    path=r'/',
                    path_specified=None,
                    secure=None,
                    expires=None,
                    discard=None,
                    comment=None,
                    comment_url=None,
                    rest=None,
                    rfc2109=False,
                )
                cookiejar.set_cookie(
                    cookie_item)  # Apply each cookie_item to cookiejar
    ckproc = urllib.request.HTTPCookieProcessor(cookiejar)
    opener = urllib.request.build_opener(ckproc)
    return opener
Exemplo n.º 11
0
def build_opener_with_chrome_cookies(domain=None):
    cookie_file_path = os.path.join(
        os.environ['LOCALAPPDATA'], r'Google\Chrome\User Data\Default\Cookies')
    cookie_file_path = r'C:\Users\KELI\AppData\Local\Google\Chrome\User Data\Default\Cookies'
    if not os.path.exists(cookie_file_path):
        raise Exception('Cookies file not exist!')
    conn = sqlite3.connect(cookie_file_path)
    sql = 'select host_key, name, value, path from cookies'
    if domain:
        sql += ' where host_key like "%{}%"'.format(domain)

    cookiejar = http.cookiejar.MozillaCookieJar()  # No cookies stored yet

    for row in conn.execute(sql):
        print(row[1], row[2])
        cookie_item = http.cookiejar.Cookie(
            version=0,
            name=row[1],
            value=row[2],
            port=None,
            port_specified=None,
            domain=row[0],
            domain_specified=None,
            domain_initial_dot=None,
            path=row[3],
            path_specified=None,
            secure=None,
            expires=None,
            discard=None,
            comment=None,
            comment_url=None,
            rest=None,
            rfc2109=False,
        )
        cookiejar.set_cookie(
            cookie_item)  # Apply each cookie_item to cookiejar

    #return urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))    # Return opener
    ckproc = urllib.request.HTTPCookieProcessor(cookiejar)
    opener = urllib.request.build_opener(ckproc)
    return opener
    conn.close()
Exemplo n.º 12
0
def build_opener_with_chrome_cookies(domain=None):
    cookie_file_path = os.path.join(os.environ["LOCALAPPDATA"], r"Google\Chrome\User Data\Default\Cookies")
    cookie_file_path = r"C:\Users\KELI\AppData\Local\Google\Chrome\User Data\Default\Cookies"
    if not os.path.exists(cookie_file_path):
        raise Exception("Cookies file not exist!")
    conn = sqlite3.connect(cookie_file_path)
    sql = "select host_key, name, value, path from cookies"
    if domain:
        sql += ' where host_key like "%{}%"'.format(domain)

    cookiejar = http.cookiejar.MozillaCookieJar()  # No cookies stored yet

    for row in conn.execute(sql):
        print(row[1], row[2])
        cookie_item = http.cookiejar.Cookie(
            version=0,
            name=row[1],
            value=row[2],
            port=None,
            port_specified=None,
            domain=row[0],
            domain_specified=None,
            domain_initial_dot=None,
            path=row[3],
            path_specified=None,
            secure=None,
            expires=None,
            discard=None,
            comment=None,
            comment_url=None,
            rest=None,
            rfc2109=False,
        )
        cookiejar.set_cookie(cookie_item)  # Apply each cookie_item to cookiejar

    # return urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))    # Return opener
    ckproc = urllib.request.HTTPCookieProcessor(cookiejar)
    opener = urllib.request.build_opener(ckproc)
    return opener
    conn.close()
Exemplo n.º 13
0
def merge_cookies(cookiejar, cookies):
    """Add cookies to cookiejar and returns a merged CookieJar.

    :param cookiejar: CookieJar object to add the cookies to.
    :param cookies: Dictionary or CookieJar object to be added.
    :rtype: CookieJar
    """
    if not isinstance(cookiejar, cookiejar.CookieJar):
        raise ValueError('You can only merge into CookieJar')

    if isinstance(cookies, dict):
        cookiejar = cookiejar_from_dict(cookies,
                                        cookiejar=cookiejar,
                                        overwrite=False)
    elif isinstance(cookies, cookiejar.CookieJar):
        try:
            cookiejar.update(cookies)
        except AttributeError:
            for cookie_in_jar in cookies:
                cookiejar.set_cookie(cookie_in_jar)

    return cookiejar
Exemplo n.º 14
0
def build_opener_with_cookies(domain=None):
    #读入cookies
    cookiejar = http.cookiejar.MozillaCookieJar()    # No cookies stored yet
    arr=getChromeCookies(domain)

    for a in arr:
        cookie_item = http.cookiejar.Cookie(
            version=0, name=str(a['name']), value=str(a['value']),
                     port=None, port_specified=None,
                     domain=str(a['hostname']), domain_specified=None, domain_initial_dot=None,
                     path=str(a['path']), path_specified=None,
                     secure=None,
                     expires=None,
                     discard=None,
                     comment=None,
                     comment_url=None,
                     rest=None,
                     rfc2109=False,
            )
        cookiejar.set_cookie(cookie_item)    # Apply each cookie_item to cookiejar
    ckproc=urllib.request.HTTPCookieProcessor(cookiejar)
    opener=urllib.request.build_opener(ckproc)
    return opener
Exemplo n.º 15
0
def get_all_bar():
    url='http://tieba.baidu.com/f/like/mylike?pn='
    page=1
    result=[]
    while(1):
        url2=url+str(page)
        print(url2)
        html=opener.open(url2)
        html=html.read()
        html=html.decode('gbk')
        result+=match_bar(html)
        if(html.find('下一页') == -1):
            break
        page+=1
    return result
def add_all_sign(bars):
    for index,i in enumerate(bars):
        result=add_sign(i)
        print("%s吧 签到成功" % i)
        sptime=random.randint(5,10)
        time.sleep(sptime)


BDUSS=''#此处请修改为自己的BDUSS
cookie=make_cookie('BDUSS',BDUSS)
cookiejar.set_cookie(cookie)


bars=get_all_bar()
add_all_sign(bars)
Exemplo n.º 16
0
    result = []
    while (1):
        url2 = url + str(page)
        print(url2)
        html = opener.open(url2)
        html = html.read()
        html = html.decode('gbk')
        result += match_bar(html)
        if (html.find('下一页') == -1):
            break
        page += 1
    return result


def add_all_sign(bars):
    for index, i in enumerate(bars):
        result = add_sign(i)
        print("%s吧 签到成功" % i)
        sptime = random.randint(5, 10)
        time.sleep(sptime)


BDUSS = 'BDUSS=MxTzZydGEwamQtRlYtYjJhcDF1ZVFZeU5Ed0R0ZDFLY3lleX5rTWdyUFc4cnRaSVFBQUFBJCQAAAAAAAAAAAEAAACie6pLu8bJq7fj0rZ2dgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANZllFnWZZRZdV'
# 此处请修改为自己的BDUSS
cookie = make_cookie('BDUSS', BDUSS)
cookiejar.set_cookie(cookie)

if __name__ == '__main__':
    _bars = get_all_bar()
    add_all_sign(_bars)