diff --git a/zhihu/auth.py b/zhihu/auth.py index 62701c6..c44dda7 100644 --- a/zhihu/auth.py +++ b/zhihu/auth.py @@ -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: @@ -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) diff --git a/zhihu/models/__init__.py b/zhihu/models/__init__.py index 7127521..ada5aff 100644 --- a/zhihu/models/__init__.py +++ b/zhihu/models/__init__.py @@ -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") diff --git a/zhihu/models/account.py b/zhihu/models/account.py index 818b6c7..da39709 100644 --- a/zhihu/models/account.py +++ b/zhihu/models/account.py @@ -10,6 +10,12 @@ class Account(Model): + def __init__(self, **kwargs): + """ + 初始化 + """ + super(Account, self).__init__(**kwargs) + def login(self, account, password, **kwargs): """ 账户登录 @@ -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: diff --git a/zhihu/models/zhihu.py b/zhihu/models/zhihu.py index 0eb271c..120745f 100644 --- a/zhihu/models/zhihu.py +++ b/zhihu/models/zhihu.py @@ -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): """