You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Userasks(Model):
def init(self, slug=None, url=None):
slug = slug if slug is not None else self._extract_slug(url)
if not slug:
raise ZhihuError("没有指定用户的的slug或者url")
self.slug = slug
super(Userasks, self).init()
@staticmethod
def _extract_id(url):
"""
从url中提取目标id
:param url:
:return:
"""
pattern = re.compile("https://www.zhihu.com/people/(\w+).*?/")
match = pattern.search(url)
return match.group(1) if match else None
def asks_list(self, **kwargs):
question_list = []
url = URL.user_asks(self.slug)
response = self._session.get(url)
soup = BeautifulSoup(response.content, "html.parser")
tag_list = soup.find_all("div", "ContentItem")
for name in tag_list.find_all("a"):
question_list.append(name.get_text())
return question_list
`
The text was updated successfully, but these errors were encountered:
我想定义一个类,用来实现获取指定用户的“提问”列表。
比如,获取用户heikehuawuya所有提问的标题。
url = https://www.zhihu.com/people/heikehuawuya/asks
我的代码都是依托zhihu-api框架,自己的代码就是asks_list()函数。
调用response = self._session.get(url)时,获取的网页内容,和浏览器获取的不一样。
调试很久,找不出原因,请大家帮忙!
代码如下。
class Userasks(Model):
def init(self, slug=None, url=None):
slug = slug if slug is not None else self._extract_slug(url)
if not slug:
raise ZhihuError("没有指定用户的的slug或者url")
self.slug = slug
super(Userasks, self).init()
`
The text was updated successfully, but these errors were encountered: