Skip to content

Commit 0787bec

Browse files
committed
update
1 parent 4062933 commit 0787bec

6 files changed

Lines changed: 58 additions & 34 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
>作者:liuzhijun
99
>微信: lzjun567
10-
>公众号:一个程序员的微站(id:VTtalk)
10+
>公众号:Python之禅(id:VTtalk)

pdf/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ python crawler.py
4848

4949
>作者:liuzhijun
5050
>微信: lzjun567
51-
>公众号:一个程序员的微站(id:VTtalk)
51+
>公众号:Python之禅(id:VTtalk)
5252
5353

zhihu/auto_login.py

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,68 @@
11
# encoding: utf-8
22
# !/usr/bin/env python
3+
"""
34
4-
import requests
5+
作者:liuzhijun
6+
微信: lzjun567
7+
公众号:Python之禅(id:VTtalk)
8+
"""
9+
import time
10+
from http import cookiejar
511

12+
import requests
613
from bs4 import BeautifulSoup
714

8-
headers = headers = {
9-
"User-Agent": "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36",
10-
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
11-
"Accept-Encoding": "gzip, deflate",
15+
headers = {
1216
"Host": "www.zhihu.com",
13-
"Upgrade-Insecure-Requests": "1",
17+
"Referer": "https://www.zhihu.com/",
18+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87'
1419
}
15-
response = requests.get("https://www.zhihu.com", headers=headers)
16-
soup = BeautifulSoup(response.content, "html.parser")
17-
xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value")
18-
print(xsrf)
19-
# print(response.content)
20-
21-
data = {
22-
"_xsrf": xsrf,
23-
"password": "xxx",
24-
"captcha": "pvRJ",
25-
"email": "xxxxxx",
26-
}
27-
# _xsrf=ef85127d992f6ea39d9817bbb7a315d4&password=lzjun854979&captcha=ENTV&email=lzjun567%40qq.com
28-
with open("text.gif", "wb") as f:
29-
f.write(requests.get("https://www.zhihu.com/captcha.gif?r=1490697036809&type=login", headers=headers).content)
3020

31-
captcha = input()
21+
# 使用登录cookie信息
22+
session = requests.session()
23+
session.cookies = cookiejar.LWPCookieJar(filename='cookies')
24+
try:
25+
session.cookies.load(ignore_discard=True)
26+
except:
27+
print("还没有cookie信息")
28+
29+
30+
def get_xsrf():
31+
response = session.get("https://www.zhihu.com", headers=headers)
32+
soup = BeautifulSoup(response.content, "html.parser")
33+
xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value")
34+
return xsrf
35+
36+
37+
def get_captcha():
38+
"""
39+
把验证码图片保存到当前目录,手动识别验证码
40+
:return:
41+
"""
42+
t = str(int(time.time() * 1000))
43+
captcha_url = 'https://www.zhihu.com/captcha.gif?r=' + t + "&type=login"
44+
r = session.get(captcha_url, headers=headers)
45+
with open('captcha.jpg', 'wb') as f:
46+
f.write(r.content)
47+
captcha = input("验证码:")
48+
return captcha
49+
3250

33-
data['captcha'] = captcha
51+
def login(email, password):
52+
login_url = 'https://www.zhihu.com/login/email'
53+
data = {
54+
'email': email,
55+
'password': password,
56+
'_xsrf': get_xsrf(),
57+
"captcha": get_captcha(),
58+
'remember_me': 'true'}
59+
response = session.post(login_url, data=data, headers=headers)
60+
login_code = response.json()
61+
print(login_code['msg'])
62+
session.cookies.save()
3463

35-
print(data)
36-
import json
37-
response = requests.post("https://www.zhihu.com/login/email", headers=headers, data=json.dumps(data))
38-
print(response.content)
39-
print(response.json().get("data").get("captcha"))
40-
print(response.json().get("data").get("account"))
41-
import re
4264

43-
s = "sdfdf 12:22:23 dsfsdf 23:23:12"
44-
print(re.compile(r"(\d{2}:\d{2}:\d{2})").findall(s))
65+
if __name__ == '__main__':
66+
email = input('email:')
67+
password = input("password:")
68+
login(email, password)

zhihu/captcha.jpg

4.52 KB
Loading

zhihu/cookies

Whitespace-only changes.

zhihu/text.gif

-3.35 KB
Binary file not shown.

0 commit comments

Comments
 (0)