File: examples.md

package info (click to toggle)
python-recipe-scrapers 15.9.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 246,580 kB
  • sloc: python: 13,214; makefile: 3
file content (77 lines) | stat: -rw-r--r-- 2,461 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Examples

!!! warning
    `recipe-scrapers` is designed to focus **exclusively on HTML parsing**.

    This core principle guides our development and support. You'll need to implement your own solution
    for fetching recipe HTMLs and managing network requests. The library works best when you
    provide both the HTML content and its source domain.


### HTTP Clients

Some Python HTTP clients you can use to retrieve HTML include:

- [requests](https://docs.python-requests.org/en/): Popular and feature-rich
- [httpx](https://www.python-httpx.org/): A fully featured HTTP client for Python 3,
- [aiohttp](https://docs.aiohttp.org/en/): Asynchronous HTTP Client/Server
- [urllib.request](https://docs.python.org/3/library/urllib.request.html): Included in Python's standard library

Please refer to their documentation to find out what options (headers, timeout configuration, proxy
support, etc) are available.

We use the built-in [urllib.requests](https://docs.python.org/3/library/urllib.request.html)
in our examples and assume HTML has been fetched successfully.

## Usage

Example of how to use the library:

```python title="Basic Usage Example" linenums="1"
from urllib.request import urlopen
from recipe_scrapers import scrape_html

url = "https://www.allrecipes.com/recipe/158968/spinach-and-feta-turkey-burgers/"
html = urlopen(url).read().decode("utf-8")  # retrieves the recipe webpage HTML
scraper = scrape_html(html, org_url=url)

# Extract recipe information
scraper.title()
scraper.instructions()
scraper.links()
scraper.to_json()
scraper.nutrients()

# To see all available methods
help(scraper)
```

For optimal results, always provide both the HTML content and its original URL.
This helps the library correctly parse website-specific elements.

## Available Methods

Recipe websites vary in the amount of information they provide. While some offer
comprehensive details like nutritional information (`.nutrients()`), others may
not.

### Core Methods

These methods are available for all the supported websites:

!!! warning "Under Construction"
    This documentation section is currently being updated and improved.


### Optional Methods

These additional methods are available for some websites:

!!! warning "Under Construction"
    This documentation section is currently being updated and improved.


## Common Patterns

!!! warning "Under Construction"
    This documentation section is currently being updated and improved.