-
Notifications
You must be signed in to change notification settings - Fork 255
增加了无需登陆情况下,根据用户名获取用户关注情况的api #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a0f0f8a
7137b55
109580c
6566b4f
08308eb
b379c39
7cdb911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,3 +120,25 @@ def _execute(self, method="post", url=None, data=None, data_type=RequestDataType | |
| else: | ||
| r = getattr(self._session, method)(url, data=data, **kwargs) | ||
| return r | ||
| def _get_token(self, Name=None, Headers=settings.HEADERS): | ||
| assert type(Name) == str, "name的类型必须为字符串形式" | ||
| """ | ||
| 根据用户名,获取最相关的用户的token。 | ||
| :return: token | ||
| >>> _get_token(Name="高日日") | ||
| >>> 返回/people/gao-ri-ri-78 | ||
| """ | ||
| url = 'https://www.zhihu.com/search?type=people&q={}'.format(Name) | ||
| data = requests.get(url, headers=Headers) | ||
| soup = BeautifulSoup(data.text, 'lxml') | ||
| yonghu = soup.select('a[class="name-link author-link"]') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| pattern = '<a.*?href="([^"]*)".*?>(?:[\S\s]*?)</a>' | ||
| token = None | ||
| for i in yonghu: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 两个点。
|
||
| i = str(i) | ||
| mat = re.findall(pattern, i) | ||
| token = mat | ||
| if token[0]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的逻辑是,找到第一个 |
||
| break | ||
|
|
||
| return token[0] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,3 +106,37 @@ def unfollow(self, user_slug=None, profile_url=None, **kwargs): | |
| return response.json() | ||
| else: | ||
| raise ZhihuError("操作失败:%s" % response.text) | ||
|
|
||
| def follows(self, user_name=None): | ||
| """ | ||
| 用户所关注人数,被关注的人数 | ||
| :param user_slug: | ||
| :param profile_url: | ||
| :return: {"follower_count": int} | ||
|
|
||
| >>> follows(user_name = "高日日") | ||
| """ | ||
| if not user_name: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 既然要求一定要填 |
||
| raise ZhihuError("至少指定一个关键字参数") | ||
|
|
||
| user_slug = self._get_token(user_name) | ||
|
|
||
| response = requests.get(URL.user_followed_number(user_slug), headers={'User-Agent': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 恩,因为作者之前的失效了,我自己测试随便写了一个。 |
||
| 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' | ||
| }) | ||
| if response.ok: | ||
| soup = BeautifulSoup(response.text, 'lxml') | ||
| init_data_renshu = soup.select('div[class="NumberBoard-value"]') | ||
|
|
||
| dicts = dict() | ||
| counts = 0 | ||
| for i in init_data_renshu: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里直接写两遍
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry,比较菜 |
||
| if counts < 1: | ||
| dicts["following"] = str(i.get_text()) | ||
| else: | ||
| dicts["followers"] = str(i.get_text()) | ||
| counts += 1 | ||
|
|
||
| print(dicts) | ||
| else: | ||
| raise ZhihuError("操作失败:%s" % response.text) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
followvsfollows,小伙子改个名吧。。。