zhihu

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package zhihu is the library behind the zhihu command line: the HTTP client, request shaping, and the typed data models for the Zhihu hot list (热榜) and search suggestions.

The Android User-Agent trick: the hot-list and suggest endpoints work without a login session when the client identifies itself as the Zhihu Android app. The Client sets both User-Agent and x-app-za on every request.

Index

Constants

View Source
const (

	// Host is the site this client talks to.
	Host = "zhihu.com"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to Zhihu over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Hot

func (c *Client) Hot(ctx context.Context, limit int) ([]HotItem, error)

Hot fetches the current Zhihu hot list. It returns at most limit items (pass 0 for the API default of 50).

func (*Client) Suggest

func (c *Client) Suggest(ctx context.Context, query string, limit int) ([]Suggestion, error)

Suggest fetches autocomplete suggestions for the given query. It returns at most limit items.

type Config

type Config struct {
	BaseURL   string
	UserAgent string
	AppZA     string
	Rate      time.Duration
	Timeout   time.Duration
	Retries   int
}

Config holds all tunable parameters for the Client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with the Android UA and sensible defaults.

type Domain

type Domain struct{}

Domain is the zhihu driver.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns an input into the canonical (type, id). Zhihu questions live at /question/{id}.

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate returns the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type HotItem

type HotItem struct {
	Rank    int    `json:"rank"`
	Title   string `json:"title"`
	Heat    string `json:"heat"`    // e.g. "951 万热度"
	Answers int    `json:"answers"` // answer_count
	URL     string `json:"url"`     // https://www.zhihu.com/question/{id}
}

HotItem is one entry from the Zhihu hot list (热榜).

type Suggestion

type Suggestion struct {
	Rank  int    `json:"rank"`
	Query string `json:"query"` // suggested search string
}

Suggestion is one autocomplete result from the Zhihu search suggestion API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL