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"))