ZhihuAccount Interface

exception zhihuAPI.account.ZhihuAPIError
class zhihuAPI.account.ZhihuAccount(path)

A class that provides functions to access the APIs. ZhihuAccount should be initialized with a cookie file.

action(cat, item, _id, _data=None, _params=None)

Executes actions for specified APIs.

Parameters:
  • cat – The category of the API (first level key).
  • item – The name of the API (second level key).
  • _id – The ID to fill for the API url. Could be empty for some APIs.
  • _data – A dictionary of data to pass into payload.
  • _params – Additional parameters.
Returns:

Action result or query result.

Return type:

dictionary

get_all_pages_json(cat, item, _id, _params=None)

Executes GET for specified API, returns the complete list of data if paging is present.

Parameters:
  • cat – The category of the API (first level key).
  • item – The name of the API (second level key).
  • _id – The ID to fill for the API url. Could be empty for some APIs.
  • _params – Additional parameters.
Returns:

Complete results from the query, type depends on paging information.

Return type:

dictionary or list

get_html(url)

Gets HTML from url. Removes scripts.

Parameters:url – The url to save.
Returns:Contains the HTML code.
Return type:string
get_page_json(cat, item, _id, _params=None)

Executes GET for specified API.

Parameters:
  • cat – The category of the API (first level key).
  • item – The name of the API (second level key).
  • _id – The ID to fill for the API url. Could be empty for some APIs.
  • _params – Additional parameters.
Returns:

Query result.

Return type:

dictionary

get_url_json(url, _params=None)

Executes GET on specified url.

Parameters:
  • url – The API url to fetch data from.
  • _params – Additional parameters. Note that zhihu uses comma separated parameters, for example: {‘include’:[‘a,b’]}
Returns:

Query result.

Return type:

dictionary

save_all_html(item, user_id, save_dir, author=None)

Saves all HTMLs of a category of a user.

Parameters:
  • item – The category to save. Available: [‘Questions’, ‘Answers’, ‘Articles’]
  • user_id – The user url token.
  • save_dir – The path to save.
  • author – Save files within a folder with author’s name.
Returns:

None

save_all_pages_json(cat, item, _id, save_dir, _params=None, time_stamp=False)

Saves the result of get_all_pages_json

Parameters:
  • cat – The category of the API (first level key).
  • item – The name of the API (second level key).
  • _id – The ID to fill for the API url. Could be empty for some APIs.
  • save_dir – The directory to save files.
  • _params – Additional parameters.
  • time_stamp – Add time stamp in the file name.
Returns:

None

save_html(url, item, save_dir, author=None)

Saves HTML to file.

Parameters:
  • url – The url to save.
  • item – The category to save. Available: [‘Questions’, ‘Answers’, ‘Articles’]
  • save_dir – The path to save.
  • author – Save files within a folder with author’s name.
Returns:

None