zoukankan      html  css  js  c++  java
  • requests库的get请求,带有cookies

    (一)如何带cookies请求

    方法一:headers中带cookies

    #coding:utf-8
    import requests
    import re
    
    # 构建url
    url = 'http://www.renren.com/923768535'
    
    # 构建请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
        'Cookie': 'anonymid=j6c96snx6i82ml; _r01_=1; _ga=GA1.2.1361939841.1504226199; depovince=GUZ; JSESSIONID=abcjqMbx6AsKFexLLwC_v; jebe_key=2b511d4c-0b0e-4e77-bcbd-28616d344a3d%7Ceda913e449d4d8cd6ac80727da63a1fe%7C1510156700139%7C1%7C1510156700366; ch_id=10016; jebecookies=5dddc92f-140c-41ef-be32-c6e023d9a0f2|||||; ick_login=28789547-d18b-4142-af78-35fb306907a3; _de=4F1FF60C280AA48B2CD1201DB4C6DF4A; p=1d40e67e3bf905bdc36671554ad798895; first_login_flag=1; ln_uact=17173805860; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; t=3df131639fc50c5fd3189244f8fcd7ba5; societyguester=3df131639fc50c5fd3189244f8fcd7ba5; id=923768535; xnsid=617e5204; ver=7.0; loginfrom=null; wp_fold=0'
    }
    
    # 发起请求获取响应
    response = requests.get(url,headers=headers)
    
    
    # 验证是否登陆成功
    print (re.findall('迷途',response.content.decode()))
    print (response.url,response.status_code)
    

      

    方法二:get方法参数中带cookies

    #coding:utf-8
    import requests
    import re
    # 构建url
    url = 'http://www.renren.com/923768535'
    
    # 构建请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
    }
    
    # 构建cookies
    temp = 'anonymid=j6c96snx6i82ml; _r01_=1; _ga=GA1.2.1361939841.1504226199; depovince=GUZ; JSESSIONID=abcjqMbx6AsKFexLLwC_v; jebe_key=2b511d4c-0b0e-4e77-bcbd-28616d344a3d%7Ceda913e449d4d8cd6ac80727da63a1fe%7C1510156700139%7C1%7C1510156700366; ch_id=10016; jebecookies=5dddc92f-140c-41ef-be32-c6e023d9a0f2|||||; ick_login=28789547-d18b-4142-af78-35fb306907a3; _de=4F1FF60C280AA48B2CD1201DB4C6DF4A; p=1d40e67e3bf905bdc36671554ad798895; first_login_flag=1; ln_uact=17173805860; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; t=3df131639fc50c5fd3189244f8fcd7ba5; societyguester=3df131639fc50c5fd3189244f8fcd7ba5; id=923768535; xnsid=617e5204; ver=7.0; loginfrom=null; wp_fold=0'
    cookies = {}
    for i in temp.split('; '):
        cookies[i.split('=')[0]] = i.split('=')[1]
    # print  (cookies)
    
    # 发送请求获取响应
    response = requests.get(url,headers=headers,cookies=cookies)
    # 验证登录
    print (re.findall('迷途',response.content.decode()))
    print (response.url,response.status_code)
    

      

    (二)session的状态保持。

    有了session后,第二次请求是,就不用再传cookies、headers了。

    #coding:utf-8
    import requests
    import re
    
    # 构建url
    url = 'http://www.renren.com/PLogin.do'
    # 构建headers
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
    }
    # 构建表单数据
    post_data = {
        'email': '17173805860',
        'password': '1qaz@WSX3edc'
    }
    
    # 构建session对象
    session = requests.session()
    
    # 发送请求模拟登陆
    response = session.post(url, headers=headers, data=post_data)
    print (response.url)
    # 跳转新鲜事
    response1 = session.get('http://www.renren.com/923768535')
    # 验证登录
    print (re.findall('迷途',response1.content.decode()))
    

      

  • 相关阅读:
    Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
    MySQL源码分析以及目录结构
    mysql分表的三种方法
    Hadoop学习
    关系型数据库ACID
    九种基本数据类型和它们的封装类
    java中堆和栈的区别
    软件测试-----Graph Coverage作业
    Lab1--关于安装JUnit的简要描述
    动态导入(import)和静态导入(import)的区别
  • 原文地址:https://www.cnblogs.com/andy9468/p/8306929.html
Copyright © 2011-2022 走看看