SoFunction
Updated on 2024-11-17

python using browser cookies to simulate logged-in users accessing Zhihua

First, log in to Zhihu on Firefox, and then use the Firefox browser plug-in Httpfox to get the GET request cookie, here note that the use of the status value of 200 (get success) of a GET.

Copy the cookies out, note that this line is very long, don't add line breaks artificially. Also, the cookie is in double quotes, so copy it into the code and wrap it in single quotes.

Use the code below to verify that the request is from a simulated logged-in user:

import requests
import re
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0',
'Cookie':'q_c1=034d8149e54a425496b7dd78be571390|1496424997000|1496424997000; r_cap_; cap_; d_c0="AEACLgSM2guPTmR8y-3r_XCeIy3RVmMIrQ0=|1496424997"; __utma=51854390.870770348.1496424950.1496483801.1496485796.4; __utmz=51854390.1496485796.4.=baidu|utmccn=(organic)|utmcmd=organic; __utmv=51854390.100--|2=registration_date=20141101=1^3=entry_date=20141101=1; _zap=5b305d08-cca7-4182-b1a8-1d8190e94a3b; aliyungf_tc=AQAAAFdv8y0T4AMAj5NK3+HVvqiouPgb; acw_tc=AQAAAHPUpVJ7LQQAj5NK35xm3ILOPUBu; _xsrf=85bb3aa751345649abdd275cb42ed704; __utmc=51854390; capsion_ticket="2|1:0|10:1496486629|14:capsion_ticket|44:ODJkNDE0MDQ1MjNmNDYwZTlhZGViZWNhNWNlZDI4Y2E=|6ded3f3e82c25526f236a4bd135705bb334e25d8df96750d89afa5ae4ab49a04"; __utmb=51854390.8.10.1496485796; __utmt=1; z_c0=Mi4wQUFCQVJBVV9BQUFBUUFJdUJJemFDeGNBQUFCaEFsVk56U0phV1FCdTNCeVduZVZ3X0hweWxnWTRIeTZmMmtyUEFn|1496487376|d6107bbdbb3ccd015757953a40ee1ecedae6834c'
}
r = ("/question/20273782", headers = headers)
text = 
(r"Join the Knowledge").search(text)

If the user is logged in, the response contains the username. If you are not logged in, the response contains "Login", "Join Knowledge" , and can be judged accordingly.

Cookies have a different value each time you log in and need to be recaptured when you log out and log back in.

The above this python using browser cookies to simulate the logged in user to visit the Zhihu is all that I have shared with you, I hope to give you a reference, and I hope that you will support me more.