Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions zhihu/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@

from zhihu.models.account import Account

"""
from zhihu.settings import HEADERS
以该形式导入的HEADERS,作为参数传入时
在获取xsrf时出了问题

def _get_xsrf(self, url=None, **kwargs):
response = self._session.get(url or URL.index(), **kwargs)
soup = BeautifulSoup(response.content, "html.parser")
xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value")
return xsrf

这里soup.find(...)返回的是 NoneType

"""
agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36'
headers = {
"Host": "www.zhihu.com",
"Referer": "https://www.zhihu.com/",
'User-Agent': agent
}

try:
input = raw_input # py2
except:
Expand All @@ -18,11 +39,11 @@ def need_login(func):

def wrapper(self, *args, **kwargs):
success = False
if 'z_c0' not in requests.utils.dict_from_cookiejar(self._session.cookies):
if 'd_c0' not in requests.utils.dict_from_cookiejar(self._session.cookies):
while not success:
account = input("请输入Email或者手机号码:")
password = input("请输入密码:")
success = Account().login(account, password)
success = self.login(account, password, headers = headers)
if success:
self._session.cookies.load(ignore_discard=True)

Expand Down
1 change: 1 addition & 0 deletions zhihu/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def _get_xsrf(self, url=None, **kwargs):
:param kwargs:
:return:
"""
print(kwargs)
response = self._session.get(url or URL.index(), **kwargs)
soup = BeautifulSoup(response.content, "html.parser")
xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value")
Expand Down
10 changes: 9 additions & 1 deletion zhihu/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@


class Account(Model):
def __init__(self, **kwargs):
"""
初始化
"""
super(Account, self).__init__(**kwargs)

def login(self, account, password, **kwargs):
"""
账户登录
Expand Down Expand Up @@ -51,7 +57,9 @@ def _login_with_email(self, email, password, **kwargs):

def _login_execute(self, url=None, data=None, **kwargs):

r = super(Account, self)._execute(method="post", url=url, data=data, data_type=RequestDataType.FORM_DATA,
#r = super(Account, self)._execute(method="post", url=url, data=data, data_type=RequestDataType.FORM_DATA,
# **kwargs)
r = self._execute(method="post", url=url, data=data, data_type=RequestDataType.FORM_DATA,
**kwargs)

if r.ok:
Expand Down
6 changes: 5 additions & 1 deletion zhihu/models/zhihu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
from zhihu.models import Model
from zhihu.url import URL

from zhihu.models import account

class Zhihu(Model):
class Zhihu(account.Account):
def __init__(self, **kwargs):
super(Zhihu, self).__init__(**kwargs)

@need_login
def send_message(self, content, user_id=None, profile_url=None, user_slug=None, **kwargs):
"""
Expand Down