Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# API reference

All names are in the `pywikihow` package. The exceptions are in `pywikihow.exceptions`.

## `HowTo(url="http://www.wikihow.com/Special:Randomizer", lazy=True)`

One WikiHow article. With `lazy=True`, the object downloads and parses the page at the first access to `url`, `title`, `intro` or `steps`. With `lazy=False`, it parses the page in the constructor.

| Member | Value |
| :--- | :--- |
| `url` | The article URL |
| `title` | The article title |
| `intro` | The article intro text |
| `steps` | A list of `HowToStep` objects, in order |
| `n_steps` | The number of steps. It does not start a parse, so it is `0` until the page is parsed |
| `summary` | The title, then one line `<number> - <step summary>` for each step |
| `as_dict()` | A dict with the keys `title`, `url`, `intro`, `n_steps` and `steps` |
| `print(extended=False)` | Prints `summary`. With `extended=True`, prints the title, the intro and each step with its description |

## `HowToStep`

One step of an article.

| Member | Value |
| :--- | :--- |
| `number` | The step number |
| `summary` | The short text of the step |
| `description` | The long text of the step |
| `picture` | The URL of the step illustration, or `None` |
| `part` | The name of the parent part, for an article that divides its steps into parts |
| `title` | The step title |
| `as_dict()` | A dict with the keys `number`, `summary`, `description`, `picture`, `part` and `title` |
| `print(extended=False)` | Prints `<number> - <summary>`. With `extended=True`, also prints the description |

## Search and random lookup

| Function | Result |
| :--- | :--- |
| `RandomHowTo(lang="en")` | A `HowTo` for a random article |
| `WikiHow.search(search_term, max_results=-1, lang="en")` | A generator of `HowTo` objects. A value of `max_results` below 1 gives all results |
| `search_wikihow(query, max_results=10, lang="en")` | A list of `HowTo` objects |

Search skips each result page that it cannot parse.

## Languages

The `lang` argument selects a WikiHow site from `WikiHow.lang2url`. The functions use only the part before the first `-` and change it to lowercase, so `pt-PT` selects `pt`. The keys are: `en`, `es`, `pt`, `it`, `fr`, `ru`, `de`, `zh`, `nl`, `cz`, `id`, `jp`, `hi`, `th`, `ar`, `ko`, `tr` and `vn`.

## Exceptions

| Exception | Cause |
| :--- | :--- |
| `UnsupportedLanguage` (a `ValueError`) | The `lang` value is not a key of `WikiHow.lang2url` |
| `ParseError` (a `RuntimeError`) | The page does not have the expected WikiHow structure, for example a page that does not exist |

## Network and cache

Each page request goes through a `requests_cache` session in memory. A cached page expires after one hour.

## Example

This is `examples/step_pictures.py`. It needs network access to WikiHow, and its output is not shown here.

```python
from pywikihow import HowTo

how_to = HowTo("https://www.wikihow.com/Tie-a-Tie")

for step in how_to.steps:
if step.picture:
print(step.number, step.summary, "->", step.picture)
```
9 changes: 9 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PyWikiHow documentation

PyWikiHow is an unofficial Python API for WikiHow. It scrapes a WikiHow page with BeautifulSoup and returns the structured article data. It also supports search and random-article lookup.

For installation and usage examples, read the [README](../README.md).

| Page | Contents |
| :--- | :--- |
| [API reference](api.md) | The `HowTo` and `HowToStep` classes, search and random lookup, languages and exception types. |
Loading