zoukankan      html  css  js  c++  java
  • python2中urllib2模块带cookies使用方法

    #!/usr/bin/python
    # coding=utf-8
    #############方式1#########################
    import urllib2
    
    cookie = "anonymid=jn5lbcm4-5e6p6j; depovince=HUB; _r01_=1; ick_login=32e4276a-5bbf-4711-a88d-2f28630c3763; ick=3cc0f79b-01d2-485d-8640-2bcaa5021e6b; __utma=151146938.1263734026.1539323353.1539323353.1539323353.1; __utmc=151146938; __utmz=151146938.1539323353.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; first_login_flag=1; ln_uact=15827325743; ln_hurl=http://head.xiaonei.com/photos/0/0/women_main.gif; jebe_key=c7507606-efe7-4c38-8e9c-7e0012754caa%7Cb59bd55b2f9ca172123871fb5854c08d%7C1539323487713%7C1%7C1539323486869; wp_fold=0; jebecookies=3c9f3547-c4c8-4fd5-804a-d1d25bcfe3d6|||||; _de=7789F161F392486A84BB56A3325ED357; p=7f56135138ee5b2b9670a6396256e15b4; t=b9b13d669f798463bb9161405ceda0544; societyguester=b9b13d669f798463bb9161405ceda0544; id=967341964; ver=7.0; xnsid=98e715c7; loginfrom=null"
    cookie_dict = {i.split("=")[0]: i.split("=")[1] for i in cookie.split("; ")}
    print("cookie_dict", cookie_dict)
    opener = urllib2.build_opener()
    opener.addheaders.append(('Cookie', cookie))
    response = opener.open("http://www.renren.com/941954027/profile")
    html = response.read().decode('utf-8')
    
    with open("./test_html/renren_urllib2_1.html", "w") as f:
        f.write(html.encode("utf-8"))
    
    
    ############方式2##################
    import urllib2
    
    opener = urllib2.build_opener()
    str = "anonymid=jn5lbcm4-5e6p6j; depovince=HUB; _r01_=1; ick_login=32e4276a-5bbf-4711-a88d-2f28630c3763; ick=3cc0f79b-01d2-485d-8640-2bcaa5021e6b; __utma=151146938.1263734026.1539323353.1539323353.1539323353.1; __utmc=151146938; __utmz=151146938.1539323353.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; first_login_flag=1; ln_uact=15827325743; ln_hurl=http://head.xiaonei.com/photos/0/0/women_main.gif; jebe_key=c7507606-efe7-4c38-8e9c-7e0012754caa%7Cb59bd55b2f9ca172123871fb5854c08d%7C1539323487713%7C1%7C1539323486869; wp_fold=0; jebecookies=3c9f3547-c4c8-4fd5-804a-d1d25bcfe3d6|||||; _de=7789F161F392486A84BB56A3325ED357; p=7f56135138ee5b2b9670a6396256e15b4; t=b9b13d669f798463bb9161405ceda0544; societyguester=b9b13d669f798463bb9161405ceda0544; id=967341964; ver=7.0; xnsid=98e715c7; loginfrom=null;"
    opener.addheaders.append(('Cookie', str))
    response = opener.open("http://www.renren.com/941954027/profile")
    html = response.read().decode('utf-8')
    
    with open("./test_html/renren_urllib2_2.html", "w") as f:
        f.write(html.encode("utf-8"))
    
    ##########################################
    import urllib2
    
    opener = urllib2.build_opener()
    str = "csrftoken=XeyGDIvTQqIYceqiYTAtvphEj916AkbK; sessionid=15g6yuqojngdo35dz1djwzisvm7q2jwt;"
    # str = "anonymid=jn5lbcm4-5e6p6j; depovince=HUB; _r01_=1; ick_login=32e4276a-5bbf-4711-a88d-2f28630c3763; ick=3cc0f79b-01d2-485d-8640-2bcaa5021e6b; __utma=151146938.1263734026.1539323353.1539323353.1539323353.1; __utmc=151146938; __utmz=151146938.1539323353.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; first_login_flag=1; ln_uact=15827325743; ln_hurl=http://head.xiaonei.com/photos/0/0/women_main.gif; jebe_key=c7507606-efe7-4c38-8e9c-7e0012754caa%7Cb59bd55b2f9ca172123871fb5854c08d%7C1539323487713%7C1%7C1539323486869; wp_fold=0; jebecookies=3c9f3547-c4c8-4fd5-804a-d1d25bcfe3d6|||||; _de=7789F161F392486A84BB56A3325ED357; p=7f56135138ee5b2b9670a6396256e15b4; t=b9b13d669f798463bb9161405ceda0544; societyguester=b9b13d669f798463bb9161405ceda0544; id=967341964; ver=7.0; xnsid=98e715c7; loginfrom=null;"
    opener.addheaders.append(('Cookie', str))
    response = opener.open(
        "http://10.67.19.77:9006/bsa_tsa_cqmc/api/v1/analysis/searchEvent/?endTime=1539330356&page=1&pageSize=10&startTime=1536738356&typename=event")
    html = response.read().decode('utf-8')
    
    with open("./test_html/renren_urllib2_3.json", "w") as f:
        f.write(html.encode("utf-8"))
    View Code
  • 相关阅读:
    【SQL Server性能优化】SQL Server 2008之表压缩
    SQL Server 锁的排队机制
    通过DAC来连接SQL Server
    通过SQL Server的数据库邮件功能功能发送邮件
    【SQL 编程你也行】BOM按节点排序
    【SQL Server数据迁移】32位的机器:SQL Server中查询ORACLE的数据
    《女孩梦三十》
    在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串、补足行数)
    【SQL Server高可用性】数据库镜像:同一台机器的两个不同实例上配置数据库镜像 + 另一台见证服务器
    【SQL Server高可用性】数据库复制:修改表结构、新增表、新增存储过程 会被复制到订阅服务器?
  • 原文地址:https://www.cnblogs.com/cerofang/p/9779547.html
Copyright © 2011-2022 走看看