Python3 requests实现cookie登陆--手动
工作流程
手动登陆人人网地址,拿到一个cookie。把cookie拷下来,手动定义一个headers,发起请求
import requests def xiaodai(): url = 'http://www.renren.com/974598244/profile' # 浏览器访问的网页的cookie headers = { 'Cookie': 'anonymid=kbbsnsnb-olb306; depovince=CQ; _r01_=1; JSESSIONID=abcn-06FJhQKRgpHKfNkx; taihe_bi_sdk_uid=34b642138c2ba4bcb9bf906f115fe68a; taihe_bi_sdk_session=b3c78f30afd84d2c50aa2fda380ee821; ick_login=d0630f10-0c06-4773-903d-e6c1a2f22ea1; ick=e1c9997f-799a-4f49-bbca-e91c107329a4; XNESSESSIONID=9ae9dc505a5a; jebe_key=f1096580-bc52-4a07-afae-4ca1f1cf5ee4%7C31aa984d1911911ca8bb1031c1d7ed61%7C1591941303204%7C1%7C1591941303562; jebe_key=f1096580-bc52-4a07-afae-4ca1f1cf5ee4%7C31aa984d1911911ca8bb1031c1d7ed61%7C1591941303204%7C1%7C1591941303564; wp_fold=0; jebecookies=101762a8-2d7c-4979-b985-18d3f11ca434|||||; _de=657B561686DA077D3DB73AE72B63CE52; p=56db424b621c179e440a7e825fd3a8684; first_login_flag=1; ln_uact=18580748731; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; t=9b856dd47672960497f85e7e751e87b04; societyguester=9b856dd47672960497f85e7e751e87b04; id=974598244; xnsid=58b46c6b; ver=7.0; loginfrom=null' } # 发起一个get请求,并且带上headers里面的cookie值 rsp = requests.get(url, headers=headers) # 调用response中的.text方法 html = rsp.text print(type(html)) # 存入本地文件html.html with open('html.html', 'w', encoding='utf-8') as f: f.write(html) # if __name__ == '__main__': # 执行函数 xiaodai()
打印结果