Requests using cookies to log on to the website

Source: Internet
Author: User

Take login Zhihu as an example

Method 1: Manually construct the Requestscookiejar object

After manually logging in to Zhihu, copy all cookies from the F12 's network column and assign them to the cookie variables.

cookies= ' _zap=01f158f1-ff75-438d-a8b3-cba055f449c1; d_c0= ' aicj1mstoa2ptkxzalxqcwepbfmeb7ijcjy=|1526867722 ";  q_c1=113a730a7a4c40d0bffd84d42a551489|1527563854000|1524791871000; hm_lvt_ ba7c84ce230944c13900faeba642b2b4=1527241782,1527563857,1527586103,1527743355; _xsrf= C92a1b0b-8efa-4ddf-b2b3-07cf5610b11c; l_n_c=1; l_cap_id= "Ztc0mjgxyjyxmwnknduxmgfmmgq5y2zlyme2ndgxymi =|1529994964|cae58c665907a32ef8d27ad74a51eca98fe4f97a ";  r_cap_id=" mtjjymjmyjizytbhngq4mwiyzmqzzdzmnwjhngy5mjc=|1529994964|3d6f86e368a9f9ac5cb945f334656c07d408c613 ";  cap_id = "zwrinzhhnjg3yjfhndhimweymge4ngiyywq0m2e4ytq=|1529994964|a48525f631fa3ace981207f6a748b9936f38ce73";  n_c= 1; __utma=51854390.1837640810.1529994966.1529994966.1529994966.1; __utmc=51854390; __utmz= 51854390.1529994966.1.1.utmcsr=zhihu.com|utmccn= (Referral) |utmcmd=referral|utmcct=/; __utmv=51854390.000- -|3=entry_date=20180427=1; tgw_l7_route=860ecf76daf7b83f5a2f2dc22dccf049; capsion_ticket= "2|1:0|10:1529998680|14:capsion_ticket|44:mzi5ndi3nmy0zdg2ndhlogiwntrhyzm1odhlntjiyty=| 177bd3ed5d98ccee8ca6b291d12240aaedaf6f1325fcb08457c912e4fbfeef46 ";  z_c0=" 2|1:0|10:1529998692|4:z_c0|92: mi4xlvv3eufbqufbqufbz0tqvxl5mmdeu1lbqufcz0fsvk5arhnmwefccmdpu2jnoddhywgym00tz1oxt3l5dnrimmp3| ccb1e3c59663aec502eddd76f154e65479fef3005490def7b35d8e2956a07b8a "' headers={' host ': ' www.zhihu.com ', ' user-agent ' : ' mozilla/5.0  (windows nt 10.0; win64; x64)  AppleWebKit/537.36  (khtml,  like gecko)  chrome/67.0.3396.87 safari/537.36 '}jar=requests.cookies.requestscookiejar () For cookie in cookies.split (';'):     key,value=cookie.split (' = ', 1)      jar.set (Key,value) r=requests.get (' https://www.zhihu.com ', Headers=headers,cookies=jar)   Print (R.text)

2. Direct use of cookies within the browser

headers={' Cookie ': ' _zap=01f158f1-ff75-438d-a8b3-cba055f449c1; d_c0= ' aicj1mstoa2ptkxzalxqcwepbfmeb7ijcjy=| 1526867722 ";  q_c1=113a730a7a4c40d0bffd84d42a551489|1527563854000|1524791871000; hm_lvt_ ba7c84ce230944c13900faeba642b2b4=1527241782,1527563857,1527586103,1527743355; _xsrf= C92a1b0b-8efa-4ddf-b2b3-07cf5610b11c; l_n_c=1; l_cap_id= "Ztc0mjgxyjyxmwnknduxmgfmmgq5y2zlyme2ndgxymi =|1529994964|cae58c665907a32ef8d27ad74a51eca98fe4f97a ";  r_cap_id=" mtjjymjmyjizytbhngq4mwiyzmqzzdzmnwjhngy5mjc=|1529994964|3d6f86e368a9f9ac5cb945f334656c07d408c613 ";  cap_id = "zwrinzhhnjg3yjfhndhimweymge4ngiyywq0m2e4ytq=|1529994964|a48525f631fa3ace981207f6a748b9936f38ce73";  n_c= 1; __utma=51854390.1837640810.1529994966.1529994966.1529994966.1; __utmc=51854390; __utmz= 51854390.1529994966.1.1.utmcsr=zhihu.com|utmccn= (Referral) |utmcmd=referral|utmcct=/; __utmv=51854390.000- -|3=entry_date=20180427=1; tgw_l7_route=860ecf76daf7b83f5a2f2dc22dccf049; capsIon_ticket= "2|1:0|10:1529998680|14:capsion_ticket|44:mzi5ndi3nmy0zdg2ndhlogiwntrhyzm1odhlntjiyty=| 177bd3ed5d98ccee8ca6b291d12240aaedaf6f1325fcb08457c912e4fbfeef46 ";  z_c0=" 2|1:0|10:1529998692|4:z_c0|92: mi4xlvv3eufbqufbqufbz0tqvxl5mmdeu1lbqufcz0fsvk5arhnmwefccmdpu2jnoddhywgym00tz1oxt3l5dnrimmp3| ccb1e3c59663aec502eddd76f154e65479fef3005490def7b35d8e2956a07b8a "', ' Host ': ' www.zhihu.com ', ' user-agent ': ' mozilla/5.0  (windows nt 10.0; win64; x64)  AppleWebKit/537.36  (khtml,  Like gecko)  chrome/67.0.3396.87 safari/537.36 '}r=requests.get (' https://www.zhihu.com ', headers= Headers


Requests using cookies to log on to the website

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.