|
1 | 1 | # encoding: utf-8 |
2 | 2 | # !/usr/bin/env python |
| 3 | +""" |
3 | 4 |
|
4 | | -import requests |
| 5 | +作者:liuzhijun |
| 6 | +微信: lzjun567 |
| 7 | +公众号:Python之禅(id:VTtalk) |
| 8 | +""" |
| 9 | +import time |
| 10 | +from http import cookiejar |
5 | 11 |
|
| 12 | +import requests |
6 | 13 | from bs4 import BeautifulSoup |
7 | 14 |
|
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 = { |
12 | 16 | "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' |
14 | 19 | } |
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) |
30 | 20 |
|
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 | + |
32 | 50 |
|
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() |
34 | 63 |
|
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 |
42 | 64 |
|
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) |
0 commit comments