1 # -*- coding: utf-8 -*-
2 __author__ = 'jinxiao'
3
4 import requests
5 try:
6 import cookielib
7 except:
8 import http.cookiejar as cookielib
9
10 import re
11
12 session = requests.session() #实例化session,下面的requests可以直接换成session
13 session.cookies = cookielib.LWPCookieJar(filename="cookies.txt") #实例化cookies,保存cookies
14 #读取cookies
15 try:
16 session.cookies.load(ignore_discard=True)
17 except:
18 print ("cookie未能加载")
19
20 #知乎一定要加上浏览器的头,其他网站不一定,一般都是要的
21 agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"
22 header = {
23 "HOST":"www.zhihu.com",
24 "Referer": "https://www.zhizhu.com",
25 'User-Agent': agent
26 }
27
28 def is_login():
29 #通过个人中心页面返回状态码来判断是否为登录状态
30 inbox_url = "https://www.zhihu.com/question/56250357/answer/148534773"
31 response = session.get(inbox_url, headers=header, allow_redirects=False) #禁止重定向,判断为是否登录
32 if response.status_code != 200:
33 return False
34 else:
35 return True
36
37 def get_xsrf():
38 #获取xsrf code
39 response = session.get("https://www.zhihu.com", headers=header)
40 match_obj = re.match('.*name="_xsrf" value="(.*?)"', response.text)
41 if match_obj:
42 return (match_obj.group(1))
43 else:
44 return ""
45
46
47 def get_index():
48 response = session.get("https://www.zhihu.com", headers=header)
49 with open("index_page.html", "wb") as f:
50 f.write(response.text.encode("utf-8"))
51 print ("ok")
52
53 def zhihu_login(account, password):
54 #知乎登录
55 if re.match("^1\d{10}",account):
56 print ("手机号码登录")
57 post_url = "https://www.zhihu.com/login/phone_num"
58 post_data = {
59 "_xsrf": get_xsrf(),
60 "phone_num": account,
61 "password": password
62 }
63 else:
64 if "@" in account:
65 #判断用户名是否为邮箱
66 print("邮箱方式登录")
67 post_url = "https://www.zhihu.com/login/email"
68 post_data = {
69 "_xsrf": get_xsrf(),
70 "email": account,
71 "password": password
72 }
73
74 response_text = session.post(post_url, data=post_data, headers=header)
75 session.cookies.save()
76
77 zhihu_login("18782902568", "admin123")
78 # get_index()
79 print(is_login())