mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-29 03:23:47 +00:00
Compare commits
47 Commits
exo2
...
0d96ff7714
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d96ff7714 | |||
| ebd9d15f77 | |||
| 8a888f583c | |||
| e6c649b433 | |||
| 3619890dc4 | |||
| 123c43aa05 | |||
| a163e7687f | |||
| 0f6eb856c6 | |||
| d62145e250 | |||
| 829c303e78 | |||
| b584f9a301 | |||
| 547c7ec4c1 | |||
| 0aa765d6a0 | |||
| 8a357abe86 | |||
|
|
2f5af5aabf | ||
| a33b484dea | |||
| dd430b9861 | |||
| 011bb6a689 | |||
| 96dbaaaaf6 | |||
| ed86e588f7 | |||
|
|
0182bbbf20 | ||
|
|
cd1e266f25 | ||
|
|
2aa99453a0 | ||
| 9f1ff1ef7b | |||
|
|
bfc39db652 | ||
|
|
717fce6ca4 | ||
| 9914e8af41 | |||
|
|
5785d571b2 | ||
| ae66e94d6c | |||
| 2bc5d57a31 | |||
| 8f21e48b28 | |||
|
|
9e0cb9737e | ||
| c62a5e6a76 | |||
|
|
8cae082344 | ||
| 9da0159869 | |||
| 5c22777c2d | |||
| 0d78b1aec3 | |||
| 168ccf88dc | |||
| 6992a0ca16 | |||
| 74482af7f0 | |||
| 76017e3ea3 | |||
|
|
683582a253 | ||
| a81d5be5a9 | |||
| 2327974b6b | |||
| f02a23f032 | |||
|
|
e2317de748 | ||
| 76475d4a2a |
52
.github/workflows/python-app.yml
vendored
52
.github/workflows/python-app.yml
vendored
@@ -5,35 +5,41 @@ name: Python application
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "main" ]
|
branches: ["main"]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "main" ]
|
branches: ["main"]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Set up Python 3.10
|
|
||||||
uses: actions/setup-python@v3
|
- name: Set up Python 3.10
|
||||||
with:
|
uses: actions/setup-python@v4
|
||||||
python-version: "3.10"
|
with:
|
||||||
- name: Install dependencies
|
python-version: "3.10"
|
||||||
run: |
|
|
||||||
python -m pip install --upgrade pip
|
- name: install dependencies
|
||||||
pip install flake8 pytest
|
run: |
|
||||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
python -m pip install --upgrade pip
|
||||||
- name: Lint with flake8
|
pip install ".[test,doc]"
|
||||||
run: |
|
|
||||||
# stop the build if there are Python syntax errors or undefined names
|
- name: Lint with flake8
|
||||||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
run: |
|
||||||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
||||||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||||||
- name: Test with pytest
|
|
||||||
run: |
|
- name: Test with pytest
|
||||||
pytest
|
run: pytest
|
||||||
|
|
||||||
|
- name: Deploy Doc
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||||
|
run: |
|
||||||
|
git config user.name github-actions
|
||||||
|
git config user.email github-actions@github.com
|
||||||
|
mkdocs gh-deploy --force
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -205,3 +205,5 @@ cython_debug/
|
|||||||
marimo/_static/
|
marimo/_static/
|
||||||
marimo/_lsp/
|
marimo/_lsp/
|
||||||
__marimo__/
|
__marimo__/
|
||||||
|
|
||||||
|
*.csv
|
||||||
1
docs/index.md
Normal file
1
docs/index.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Millesima
|
||||||
3
docs/scraper.md
Normal file
3
docs/scraper.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Scraper
|
||||||
|
|
||||||
|
::: scraper.Scraper
|
||||||
4
docs/scraperdata.md
Normal file
4
docs/scraperdata.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
# _ScraperData
|
||||||
|
|
||||||
|
::: scraper._ScraperData
|
||||||
110
main.py
110
main.py
@@ -1,110 +0,0 @@
|
|||||||
from sys import stderr
|
|
||||||
from typing import cast, Any, Dict, Optional
|
|
||||||
from requests import Response, Session
|
|
||||||
from bs4 import BeautifulSoup, Tag
|
|
||||||
from json import JSONDecodeError, loads
|
|
||||||
|
|
||||||
|
|
||||||
class Scraper:
|
|
||||||
"""
|
|
||||||
Scraper est une classe qui permet de gerer
|
|
||||||
de façon dynamique des requetes uniquement
|
|
||||||
sur le serveur https de Millesima
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self._url: str = "https://www.millesima.fr/"
|
|
||||||
self._session: Session = Session()
|
|
||||||
self._latest_request: tuple[(str, Response | None)] = ("", None)
|
|
||||||
|
|
||||||
def _request(self, subdir: str) -> Response:
|
|
||||||
target_url: str = self._url + subdir.lstrip("/")
|
|
||||||
response: Response = self._session.get(url=target_url, timeout=10)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response
|
|
||||||
|
|
||||||
def getresponse(self, subdir: str = "") -> Response:
|
|
||||||
rq_subdir, rq_response = self._latest_request
|
|
||||||
if rq_response is None or subdir != rq_subdir:
|
|
||||||
request: Response = self._request(subdir)
|
|
||||||
self._latest_request = (subdir, request)
|
|
||||||
return request
|
|
||||||
return rq_response
|
|
||||||
|
|
||||||
def getsoup(self, subdir: str = "") -> BeautifulSoup:
|
|
||||||
markup: str = self.getresponse(subdir).text
|
|
||||||
return BeautifulSoup(markup, features="html.parser")
|
|
||||||
|
|
||||||
def getjsondata(self, subdir: str = "", id: str = "__NEXT_DATA__") -> dict[str, object]:
|
|
||||||
soup: BeautifulSoup = self.getsoup(subdir)
|
|
||||||
script: Tag | None = soup.find("script", id=id)
|
|
||||||
|
|
||||||
if isinstance(script, Tag) and script.string:
|
|
||||||
try:
|
|
||||||
current_data: object = loads(script.string)
|
|
||||||
keys: list[str] = ["props", "pageProps", "initialReduxState", "product", "content"]
|
|
||||||
for key in keys:
|
|
||||||
if isinstance(current_data, dict) and key in current_data:
|
|
||||||
current_data = current_data[key]
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Clé manquante dans le JSON : {key}")
|
|
||||||
|
|
||||||
if isinstance(current_data, dict):
|
|
||||||
return cast(dict[str, object], current_data)
|
|
||||||
|
|
||||||
except (JSONDecodeError, ValueError) as e:
|
|
||||||
print(f"Erreur lors de l'extraction JSON : {e}", file=stderr)
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def prix(self, subdir: str) -> float:
|
|
||||||
"""
|
|
||||||
Retourne le prix d'une bouteille (75cl).
|
|
||||||
Les données récupérées depuis le site contiennent plusieurs formats
|
|
||||||
de vente dans la liste "items" :
|
|
||||||
- bouteille seule si nbunit=1 et equivbtl=1
|
|
||||||
-> prix direct (format vendu à l'unité).
|
|
||||||
- caisse de plusieurs bouteilles si nbunit=1
|
|
||||||
-> prix direct (format vendu à l'unité).
|
|
||||||
- formats spéciaux (magnum, impériale, etc.)sinon
|
|
||||||
-> calcul du prix unitaire : offerPrice / (nbunit * equivbtl)
|
|
||||||
|
|
||||||
Chaque item possède notamment :
|
|
||||||
- offerPrice : prix total du format proposé
|
|
||||||
- nbunit : nombre d'unités dans le format
|
|
||||||
- equivbtl : équivalent en nombre de bouteilles standard (75cl)
|
|
||||||
|
|
||||||
"""
|
|
||||||
data = self.getjsondata(subdir)
|
|
||||||
|
|
||||||
items = data.get("items")
|
|
||||||
if not isinstance(items, list) or len(items) == 0:
|
|
||||||
raise ValueError("Aucun prix disponible (items vide).")
|
|
||||||
|
|
||||||
# 1) bouteille 75cl (nbunit=1 et equivbtl=1)
|
|
||||||
for item in items:
|
|
||||||
if not isinstance(item, dict):
|
|
||||||
continue
|
|
||||||
attrs = item.get("attributes", {})
|
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
|
||||||
|
|
||||||
if nbunit == "1" and equivbtl == "1":
|
|
||||||
p = item.get("offerPrice")
|
|
||||||
if isinstance(p, (int, float)):
|
|
||||||
return float(p)
|
|
||||||
|
|
||||||
# 2) calcul depuis caisse
|
|
||||||
for item in items:
|
|
||||||
if not isinstance(item, dict):
|
|
||||||
continue
|
|
||||||
p = item.get("offerPrice")
|
|
||||||
attrs = item.get("attributes", {})
|
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
|
||||||
|
|
||||||
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
|
||||||
denom = float(nbunit) * float(equivbtl)
|
|
||||||
if denom > 0:
|
|
||||||
return round(float(p) / denom, 2)
|
|
||||||
|
|
||||||
raise ValueError("Impossible de trouver le prix unitaire.")
|
|
||||||
14
mkdocs.yml
Normal file
14
mkdocs.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
site_name: "Projet Millesima S6"
|
||||||
|
|
||||||
|
theme:
|
||||||
|
name: "material"
|
||||||
|
|
||||||
|
plugins:
|
||||||
|
- search
|
||||||
|
- mkdocstrings
|
||||||
|
|
||||||
|
markdown_extensions:
|
||||||
|
- admonition
|
||||||
|
- pymdownx.details
|
||||||
|
- pymdownx.superfences
|
||||||
|
- pymdownx.tabbed
|
||||||
12
pyproject.toml
Normal file
12
pyproject.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[project]
|
||||||
|
name = "projet-millesima-s6"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = ["requests==2.32.5", "beautifulsoup4==4.14.3", "pandas==2.3.3", "tqdm==4.67.3"]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
test = ["pytest==8.4.2", "requests-mock==1.12.1", "flake8==7.3.0"]
|
||||||
|
doc = ["mkdocs<2.0.0", "mkdocs-material==9.6.23", "mkdocstrings[python]"]
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["setuptools", "wheel"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
requests>=2.32.5
|
|
||||||
beautifulsoup4>=4.14.3
|
|
||||||
Binary file not shown.
BIN
res/projet1.pdf
BIN
res/projet1.pdf
Binary file not shown.
488
src/scraper.py
Executable file
488
src/scraper.py
Executable file
@@ -0,0 +1,488 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
|
from io import SEEK_END, SEEK_SET, BufferedWriter
|
||||||
|
from json import JSONDecodeError, loads
|
||||||
|
from os import makedirs
|
||||||
|
from os.path import dirname, exists, join, normpath, realpath
|
||||||
|
from pickle import UnpicklingError, dump, load
|
||||||
|
from sys import argv
|
||||||
|
from tqdm.std import tqdm
|
||||||
|
from typing import Any, Callable, Literal, TypeVar, cast
|
||||||
|
from bs4 import BeautifulSoup, Tag
|
||||||
|
from requests import HTTPError, Response, Session
|
||||||
|
|
||||||
|
_dir: str = dirname(realpath(__name__))
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
def _getcache(mode: Literal["rb", "wb"], fn: Callable[[Any], T]) -> T | None:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: _description_
|
||||||
|
"""
|
||||||
|
cache_dirname = normpath(join(_dir, ".cache"))
|
||||||
|
save_path = normpath(join(cache_dirname, "save"))
|
||||||
|
|
||||||
|
if not exists(cache_dirname):
|
||||||
|
makedirs(cache_dirname)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(save_path, mode) as f:
|
||||||
|
return fn(f)
|
||||||
|
except (FileNotFoundError, EOFError, UnpicklingError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def savestate(data: tuple[int, set[str]]) -> None:
|
||||||
|
def save(f: BufferedWriter) -> None:
|
||||||
|
_ = f.seek(0)
|
||||||
|
_ = f.truncate()
|
||||||
|
dump(data, f)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
|
_getcache("wb", save)
|
||||||
|
|
||||||
|
|
||||||
|
def loadstate() -> tuple[int, set[str]] | None:
|
||||||
|
return _getcache("rb", lambda f: load(f))
|
||||||
|
|
||||||
|
|
||||||
|
class _ScraperData:
|
||||||
|
"""
|
||||||
|
Conteneur de données spécialisé pour extraire les informations des dictionnaires JSON.
|
||||||
|
|
||||||
|
Cette classe agit comme une interface simplifiée au-dessus du dictionnaire brut
|
||||||
|
renvoyé par la balise __NEXT_DATA__ du site Millesima.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, data: dict[str, object]) -> None:
|
||||||
|
"""
|
||||||
|
Initialise le conteneur avec un dictionnaire de données.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data (dict[str, object]): Le dictionnaire JSON brut extrait de la page.
|
||||||
|
"""
|
||||||
|
self._data: dict[str, object] = data
|
||||||
|
|
||||||
|
def _getcontent(self) -> dict[str, object] | None:
|
||||||
|
"""
|
||||||
|
Navigue dans l'arborescence Redux pour atteindre le contenu du produit.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict[str, object] | None: Le dictionnaire du produit ou None si la structure diffère.
|
||||||
|
"""
|
||||||
|
current_data: dict[str, object] = self._data
|
||||||
|
for key in ["initialReduxState", "product", "content"]:
|
||||||
|
new_data: object | None = current_data.get(key)
|
||||||
|
if new_data is None:
|
||||||
|
return None
|
||||||
|
current_data: dict[str, object] = cast(dict[str, object], new_data)
|
||||||
|
|
||||||
|
return current_data
|
||||||
|
|
||||||
|
def _getattributes(self) -> dict[str, object] | None:
|
||||||
|
"""
|
||||||
|
Extrait les attributs techniques (notes, appellations, etc.) du produit.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict[str, object] | None: Les attributs du vin ou None.
|
||||||
|
"""
|
||||||
|
current_data: object = self._getcontent()
|
||||||
|
if current_data is None:
|
||||||
|
return None
|
||||||
|
return cast(dict[str, object], current_data.get("attributes"))
|
||||||
|
|
||||||
|
def prix(self) -> float | None:
|
||||||
|
"""
|
||||||
|
Calcule le prix unitaire d'une bouteille (standardisée à 75cl).
|
||||||
|
|
||||||
|
Le site vend souvent par caisses (6, 12 bouteilles) ou formats (Magnum).
|
||||||
|
Cette méthode normalise le prix pour obtenir celui d'une seule unité.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
float | None: Le prix calculé arrondi à 2 décimales, ou None.
|
||||||
|
"""
|
||||||
|
|
||||||
|
content = self._getcontent()
|
||||||
|
if content is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
items = content.get("items")
|
||||||
|
|
||||||
|
# Vérifie que items existe et n'est pas vide
|
||||||
|
if not isinstance(items, list) or len(items) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
prix_calcule: float | None = None
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
|
p = item.get("offerPrice")
|
||||||
|
attrs = item.get("attributes", {})
|
||||||
|
|
||||||
|
nbunit = attrs.get("nbunit", {}).get("value")
|
||||||
|
equivbtl = attrs.get("equivbtl", {}).get("value")
|
||||||
|
|
||||||
|
if not isinstance(p, (int, float)) or not nbunit or not equivbtl:
|
||||||
|
continue
|
||||||
|
|
||||||
|
nb = float(nbunit)
|
||||||
|
eq = float(equivbtl)
|
||||||
|
|
||||||
|
if nb <= 0 or eq <= 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if nb == 1 and eq == 1:
|
||||||
|
return float(p)
|
||||||
|
|
||||||
|
prix_calcule = round(float(p) / (nb * eq), 2)
|
||||||
|
|
||||||
|
return prix_calcule
|
||||||
|
|
||||||
|
def appellation(self) -> str | None:
|
||||||
|
"""
|
||||||
|
Extrait le nom de l'appellation du vin.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str | None: Le nom (ex: 'Pauillac') ou None.
|
||||||
|
"""
|
||||||
|
attrs: dict[str, object] | None = self._getattributes()
|
||||||
|
if attrs is not None:
|
||||||
|
app_dict: object | None = attrs.get("appellation")
|
||||||
|
if isinstance(app_dict, dict):
|
||||||
|
return cast(str, app_dict.get("value"))
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _getcritiques(self, name: str) -> str | None:
|
||||||
|
"""
|
||||||
|
Méthode générique pour parser les notes des critiques (Parker, Suckling, etc.).
|
||||||
|
|
||||||
|
Gère les notes simples ("95") et les plages de notes ("95-97") en faisant la moyenne.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): La clé de l'attribut dans le JSON (ex: 'note_rp').
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str | None: La note formatée en chaîne de caractères ou None.
|
||||||
|
"""
|
||||||
|
|
||||||
|
current_value: dict[str, object] | None = self._getattributes()
|
||||||
|
if current_value is not None:
|
||||||
|
app_dict: dict[str, object] = cast(
|
||||||
|
dict[str, object], current_value.get(name)
|
||||||
|
)
|
||||||
|
if not app_dict:
|
||||||
|
return None
|
||||||
|
|
||||||
|
val = cast(str, app_dict.get("value")).rstrip("+").split("-")
|
||||||
|
if len(val) > 1 and val[1] != "":
|
||||||
|
val[0] = str(round((float(val[0]) + float(val[1])) / 2, 1))
|
||||||
|
|
||||||
|
return val[0]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def parker(self) -> str | None:
|
||||||
|
"""Note Robert Parker."""
|
||||||
|
return self._getcritiques("note_rp")
|
||||||
|
|
||||||
|
def robinson(self) -> str | None:
|
||||||
|
"""Note Jancis Robinson."""
|
||||||
|
return self._getcritiques("note_jr")
|
||||||
|
|
||||||
|
def suckling(self) -> str | None:
|
||||||
|
"""Note James Suckling."""
|
||||||
|
return self._getcritiques("note_js")
|
||||||
|
|
||||||
|
def getdata(self) -> dict[str, object]:
|
||||||
|
"""Retourne le dictionnaire de données complet."""
|
||||||
|
return self._data
|
||||||
|
|
||||||
|
def informations(self) -> str:
|
||||||
|
"""
|
||||||
|
Agrège les données clés pour l'export CSV.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Ligne formatée : "Appellation,Parker,Robinson,Suckling,Prix".
|
||||||
|
"""
|
||||||
|
|
||||||
|
appellation = self.appellation()
|
||||||
|
parker = self.parker()
|
||||||
|
robinson = self.robinson()
|
||||||
|
suckling = self.suckling()
|
||||||
|
prix = self.prix()
|
||||||
|
|
||||||
|
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
||||||
|
|
||||||
|
|
||||||
|
class Scraper:
|
||||||
|
"""
|
||||||
|
Client HTTP optimisé pour le scraping de millesima.fr.
|
||||||
|
|
||||||
|
Gère la session persistante, les headers de navigation et un cache double
|
||||||
|
pour optimiser les performances et la discrétion.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
"""
|
||||||
|
Initialise l'infrastructure de navigation:
|
||||||
|
|
||||||
|
- créer une session pour éviter de faire un handshake pour chaque requête
|
||||||
|
- ajout d'un header pour éviter le blocage de l'accès au site
|
||||||
|
- ajout d'un système de cache
|
||||||
|
"""
|
||||||
|
self._url: str = "https://www.millesima.fr/"
|
||||||
|
# Très utile pour éviter de renvoyer toujours les mêmes handshake
|
||||||
|
# TCP et d'avoir toujours une connexion constante avec le server
|
||||||
|
self._session: Session = Session()
|
||||||
|
# Crée une "fausse carte d'identité" pour éviter que le site nous
|
||||||
|
# bloque car on serait des robots
|
||||||
|
self._session.headers.update(
|
||||||
|
{
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
|
||||||
|
AppleWebKit/537.36 (KHTML, like Gecko) \
|
||||||
|
Chrome/122.0.0.0 Safari/537.36",
|
||||||
|
"Accept-Language": "fr-FR,fr;q=0.9,en;q=0.8",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# Système de cache pour éviter de solliciter le serveur inutilement
|
||||||
|
# utilise pour _request
|
||||||
|
self._latest_request: tuple[(str, Response)] | None = None
|
||||||
|
# utilise pour getsoup
|
||||||
|
self._latest_soups: OrderedDict[str, BeautifulSoup] = OrderedDict[
|
||||||
|
str, BeautifulSoup
|
||||||
|
]()
|
||||||
|
|
||||||
|
def _request(self, subdir: str) -> Response:
|
||||||
|
"""
|
||||||
|
Effectue une requête GET sur le serveur Millesima.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str): Le sous-répertoire ou chemin de l'URL (ex: "/vins").
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response: L'objet réponse de la requête.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPError: Si le serveur renvoie un code d'erreur (4xx, 5xx).
|
||||||
|
"""
|
||||||
|
target_url: str = self._url + subdir.lstrip("/")
|
||||||
|
# envoyer une requête GET sur la page si erreur, renvoie un raise
|
||||||
|
response: Response = self._session.get(url=target_url, timeout=30)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response
|
||||||
|
|
||||||
|
def getresponse(self, subdir: str = "", use_cache: bool = True) -> Response:
|
||||||
|
"""
|
||||||
|
Récupère la réponse d'une page, en utilisant le cache si possible.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str, optional): Le chemin de la page.
|
||||||
|
use_cache (bool, optional): Utilise la donnée deja sauvegarder ou
|
||||||
|
écrase la donnée utilisé avec la nouvelle
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response: L'objet réponse (cache ou nouvelle requête).
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPError: Si le serveur renvoie un code d'erreur (4xx, 5xx).
|
||||||
|
"""
|
||||||
|
|
||||||
|
# si dans le cache, latest_request existe
|
||||||
|
if use_cache and self._latest_request is not None:
|
||||||
|
rq_subdir, rq_response = self._latest_request
|
||||||
|
|
||||||
|
# si c'est la meme requete et que use_cache est true,
|
||||||
|
# on renvoie celle enregistrer
|
||||||
|
if subdir == rq_subdir:
|
||||||
|
return rq_response
|
||||||
|
|
||||||
|
request: Response = self._request(subdir)
|
||||||
|
# on recrée la structure pour le systeme de cache si activer
|
||||||
|
if use_cache:
|
||||||
|
self._latest_request = (subdir, request)
|
||||||
|
|
||||||
|
return request
|
||||||
|
|
||||||
|
def getsoup(self, subdir: str, use_cache: bool = True) -> BeautifulSoup:
|
||||||
|
"""
|
||||||
|
Récupère le contenu HTML d'une page et le transforme en objet BeautifulSoup.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str, optional): Le chemin de la page.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
BeautifulSoup: L'objet parsé pour extraction de données.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPError: Si le serveur renvoie un code d'erreur (4xx, 5xx).
|
||||||
|
"""
|
||||||
|
|
||||||
|
if use_cache and subdir in self._latest_soups:
|
||||||
|
return self._latest_soups[subdir]
|
||||||
|
|
||||||
|
markup: str = self.getresponse(subdir).text
|
||||||
|
soup: BeautifulSoup = BeautifulSoup(markup, features="html.parser")
|
||||||
|
|
||||||
|
if use_cache:
|
||||||
|
self._latest_soups[subdir] = soup
|
||||||
|
|
||||||
|
if len(self._latest_soups) > 10:
|
||||||
|
_ = self._latest_soups.popitem(last=False)
|
||||||
|
|
||||||
|
return soup
|
||||||
|
|
||||||
|
def getjsondata(self, subdir: str, id: str = "__NEXT_DATA__") -> _ScraperData:
|
||||||
|
"""
|
||||||
|
Extrait les données JSON contenues dans la balise __NEXT_DATA__ du site.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str): Le chemin de la page.
|
||||||
|
id (str, optional): L'identifiant de la balise script.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPError: Erreur renvoyée par le serveur (4xx, 5xx).
|
||||||
|
JSONDecodeError: Si le contenu de la balise n'est pas un JSON valide.
|
||||||
|
ValueError: Si les clés 'props' ou 'pageProps' sont absentes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_ScraperData: Instance contenant les données extraites.
|
||||||
|
"""
|
||||||
|
|
||||||
|
soup: BeautifulSoup = self.getsoup(subdir)
|
||||||
|
script: Tag | None = soup.find("script", id=id)
|
||||||
|
|
||||||
|
if script is None or not script.string:
|
||||||
|
raise ValueError(f"le script id={id} est introuvable")
|
||||||
|
|
||||||
|
current_data: object = cast(object, loads(script.string))
|
||||||
|
|
||||||
|
for key in ["props", "pageProps"]:
|
||||||
|
if isinstance(current_data, dict) and key in current_data:
|
||||||
|
current_data = cast(object, current_data[key])
|
||||||
|
continue
|
||||||
|
raise ValueError(f"Clé manquante dans le JSON : {key}")
|
||||||
|
|
||||||
|
return _ScraperData(cast(dict[str, object], current_data))
|
||||||
|
|
||||||
|
def _geturlproductslist(self, subdir: str) -> list[dict[str, Any]] | None:
|
||||||
|
"""
|
||||||
|
Récupère la liste des produits d'une page de catégorie.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
data: dict[str, object] = self.getjsondata(subdir).getdata()
|
||||||
|
|
||||||
|
for element in ["initialReduxState", "categ", "content"]:
|
||||||
|
data = cast(dict[str, object], data.get(element))
|
||||||
|
|
||||||
|
products: list[dict[str, Any]] = cast(
|
||||||
|
list[dict[str, Any]], data.get("products")
|
||||||
|
)
|
||||||
|
|
||||||
|
if isinstance(products, list):
|
||||||
|
return products
|
||||||
|
|
||||||
|
except (JSONDecodeError, HTTPError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _writevins(self, cache: set[str], product: dict[str, Any], f: Any) -> None:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cache (set[str]): _description_
|
||||||
|
product (dict): _description_
|
||||||
|
f (Any): _description_
|
||||||
|
"""
|
||||||
|
if isinstance(product, dict):
|
||||||
|
link: Any | None = product.get("seoKeyword")
|
||||||
|
if link and link not in cache:
|
||||||
|
try:
|
||||||
|
infos = self.getjsondata(link).informations()
|
||||||
|
_ = f.write(infos + "\n")
|
||||||
|
cache.add(link)
|
||||||
|
except (JSONDecodeError, HTTPError) as e:
|
||||||
|
print(f"Erreur sur le produit {link}: {e}")
|
||||||
|
|
||||||
|
def getvins(self, subdir: str, filename: str, reset: bool = False) -> None:
|
||||||
|
"""
|
||||||
|
Scrape toutes les pages d'une catégorie et sauvegarde en CSV.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str): La catégorie (ex: '/vins-rouges').
|
||||||
|
filename (str): Nom du fichier de sortie (ex: 'vins.csv').
|
||||||
|
reset (bool): (Optionnel) pour réinitialiser le processus.
|
||||||
|
"""
|
||||||
|
# mode d'écriture fichier
|
||||||
|
mode: Literal["w", "a+"] = "w" if reset else "a+"
|
||||||
|
# titre
|
||||||
|
title: str = "Appellation,Robert,Robinson,Suckling,Prix\n"
|
||||||
|
# page du début
|
||||||
|
page: int = 1
|
||||||
|
# le set qui sert de cache
|
||||||
|
cache: set[str] = set[str]()
|
||||||
|
|
||||||
|
custom_format = "{l_bar} {bar:20} {r_bar}"
|
||||||
|
|
||||||
|
if not reset:
|
||||||
|
# appelle la fonction pour load le cache, si il existe
|
||||||
|
# pas, il utilise les variables de base sinon il override
|
||||||
|
# toute les variables pour continuer et pas recommencer le
|
||||||
|
# processus en entier.
|
||||||
|
serializable: tuple[int, set[str]] | None = loadstate()
|
||||||
|
if isinstance(serializable, tuple):
|
||||||
|
# override la page et le cache
|
||||||
|
page, cache = serializable
|
||||||
|
try:
|
||||||
|
with open(filename, mode) as f:
|
||||||
|
# check si le titre est bien présent au début du buffer
|
||||||
|
# sinon il l'ecrit, petit bug potentiel, a+ ecrit tout le
|
||||||
|
# temps a la fin du buffer, si on a ecrit des choses avant
|
||||||
|
# le titre sera apres ces données mais on part du principe
|
||||||
|
# que personne va toucher le fichier.
|
||||||
|
_ = f.seek(0, SEEK_SET)
|
||||||
|
if not (f.read(len(title)) == title):
|
||||||
|
_ = f.write(title)
|
||||||
|
else:
|
||||||
|
_ = f.seek(0, SEEK_END)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
products_list: list[dict[str, Any]] | None = (
|
||||||
|
self._geturlproductslist(f"{subdir}?page={page}")
|
||||||
|
)
|
||||||
|
if not products_list:
|
||||||
|
break
|
||||||
|
|
||||||
|
pbar: tqdm[dict[str, Any]] = tqdm(
|
||||||
|
products_list, bar_format=custom_format
|
||||||
|
)
|
||||||
|
for product in pbar:
|
||||||
|
keyword = product.get("seoKeyword", "Inconnu")[:40]
|
||||||
|
pbar.set_description(
|
||||||
|
f"Page: {page:<3} | Product: {keyword:<40}"
|
||||||
|
)
|
||||||
|
self._writevins(cache, product, f)
|
||||||
|
page += 1
|
||||||
|
except (Exception, HTTPError, KeyboardInterrupt, JSONDecodeError):
|
||||||
|
if not reset:
|
||||||
|
savestate((page, cache))
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
if len(argv) != 3:
|
||||||
|
raise ValueError(f"{argv[0]} <filename> <sous-url>")
|
||||||
|
filename = argv[1]
|
||||||
|
suburl = argv[2]
|
||||||
|
|
||||||
|
scraper: Scraper = Scraper()
|
||||||
|
scraper.getvins(suburl, filename)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERREUR: {e}")
|
||||||
35
test_main.py
35
test_main.py
@@ -1,35 +0,0 @@
|
|||||||
import json
|
|
||||||
from main import Scraper
|
|
||||||
|
|
||||||
|
|
||||||
def test_json():
|
|
||||||
scraper = Scraper()
|
|
||||||
|
|
||||||
data = scraper.getjsondata("/chateau-gloria-2016.html")
|
|
||||||
|
|
||||||
print("JSON récupéré :")
|
|
||||||
print(json.dumps(data, indent=4, ensure_ascii=False))
|
|
||||||
|
|
||||||
assert isinstance(data, dict)
|
|
||||||
assert "items" in data
|
|
||||||
|
|
||||||
|
|
||||||
def test_prix():
|
|
||||||
scraper = Scraper()
|
|
||||||
|
|
||||||
try:
|
|
||||||
p = scraper.prix("/chateau-saint-pierre-2011.html")
|
|
||||||
print("Prix unitaire =", p)
|
|
||||||
|
|
||||||
assert isinstance(p, float)
|
|
||||||
assert p > 0
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
# le vin n'est pas disponible à la vente
|
|
||||||
print("OK : aucun prix (vin indisponible, items vide)")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_json()
|
|
||||||
test_prix()
|
|
||||||
print("\nTous les tests terminés")
|
|
||||||
326
tests/test_scraper.py
Normal file
326
tests/test_scraper.py
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
from json import dumps
|
||||||
|
from unittest.mock import patch, mock_open
|
||||||
|
import pytest
|
||||||
|
from requests_mock import Mocker
|
||||||
|
from scraper import Scraper
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def mock_site():
|
||||||
|
with Mocker() as m:
|
||||||
|
m.get(
|
||||||
|
"https://www.millesima.fr/",
|
||||||
|
text=f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps({
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
"initialReduxState": {
|
||||||
|
"product": {
|
||||||
|
"content": {
|
||||||
|
"items": [],
|
||||||
|
"attributes": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
m.get(
|
||||||
|
"https://www.millesima.fr/poubelle",
|
||||||
|
text=f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>POUBELLE</h1>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps({
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
json_data = {
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
"initialReduxState": {
|
||||||
|
"product": {
|
||||||
|
"content": {
|
||||||
|
"_id": "J4131/22-11652",
|
||||||
|
"partnumber": "J4131/22",
|
||||||
|
"productName": "Nino Negri : 5 Stelle Sfursat 2022",
|
||||||
|
"productNameForSearch": "Nino Negri : 5 Stelle Sfursat 2022",
|
||||||
|
"storeId": "11652",
|
||||||
|
"seoKeyword": "nino-negri-5-stelle-sfursat-2022.html",
|
||||||
|
"title": "Nino Negri : 5 Stelle Sfursat 2022",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"_id": "J4131/22/C/CC/6-11652",
|
||||||
|
"partnumber": "J4131/22/C/CC/6",
|
||||||
|
"taxRate": "H",
|
||||||
|
"listPrice": 842,
|
||||||
|
"offerPrice": 842,
|
||||||
|
"seoKeyword": "vin-de-charazade1867.html",
|
||||||
|
"shortdesc": "Une bouteille du meilleur vin du monde?",
|
||||||
|
"attributes": {
|
||||||
|
"promotion_o_n": {
|
||||||
|
"valueId": "0",
|
||||||
|
"name": "En promotion",
|
||||||
|
"value": "Non",
|
||||||
|
"sequence": 80,
|
||||||
|
"displayable": "False",
|
||||||
|
"type": "CHECKBOX",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
"in_stock": {
|
||||||
|
"valueId": "L",
|
||||||
|
"name": "En stock",
|
||||||
|
"value": "Livrable",
|
||||||
|
"sequence": 65,
|
||||||
|
"displayable": "true",
|
||||||
|
"type": "CHECKBOX",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
"equivbtl": {
|
||||||
|
"valueId": "1",
|
||||||
|
"name": "equivbtl",
|
||||||
|
"value": "1",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
"nbunit": {
|
||||||
|
"valueId": "1",
|
||||||
|
"name": "nbunit",
|
||||||
|
"value": "1",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"stock": 12,
|
||||||
|
"availability": "2026-02-05",
|
||||||
|
"isCustomizable": False,
|
||||||
|
"gtin_cond": "",
|
||||||
|
"gtin_unit": "",
|
||||||
|
"stockOrigin": "EUR",
|
||||||
|
"isPrevSale": False,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"attributes": {
|
||||||
|
"appellation": {
|
||||||
|
"valueId": "433",
|
||||||
|
"name": "Appellation",
|
||||||
|
"value": "Madame-Loïk",
|
||||||
|
"url": "Madame-loik.html",
|
||||||
|
"isSpirit": False,
|
||||||
|
"groupIdentifier": "appellation_433",
|
||||||
|
},
|
||||||
|
"note_rp": {
|
||||||
|
"valueId": "91",
|
||||||
|
"name": "Peter Parker",
|
||||||
|
"value": "91",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
"note_jr": {
|
||||||
|
"valueId": "17+",
|
||||||
|
"name": "J. Robinson",
|
||||||
|
"value": "17+",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
"note_js": {
|
||||||
|
"valueId": "93-94.5",
|
||||||
|
"name": "J. cherazade",
|
||||||
|
"value": "93-94",
|
||||||
|
"isSpirit": False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
html_product = f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>MILLESIMA</h1>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps(json_data)}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
m.get(
|
||||||
|
"https://www.millesima.fr/nino-negri-5-stelle-sfursat-2022.html",
|
||||||
|
text=html_product,
|
||||||
|
)
|
||||||
|
|
||||||
|
html_product = f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>MILLESIMA</h1>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps(json_data)}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
list_pleine = f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>LE WINE</h1>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps({
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
"initialReduxState": {
|
||||||
|
"categ": {
|
||||||
|
"content": {
|
||||||
|
"products": [
|
||||||
|
{"seoKeyword": "/nino-negri-5-stelle-sfursat-2022.html",},
|
||||||
|
{"seoKeyword": "/poubelle",},
|
||||||
|
{"seoKeyword": "/",}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
list_vide = f"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>LE WINE</h1>
|
||||||
|
<script id="__NEXT_DATA__" type="application/json">
|
||||||
|
{dumps({
|
||||||
|
"props": {
|
||||||
|
"pageProps": {
|
||||||
|
"initialReduxState": {
|
||||||
|
"categ": {
|
||||||
|
"content": {
|
||||||
|
"products": [
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
m.get(
|
||||||
|
"https://www.millesima.fr/wine.html",
|
||||||
|
complete_qs=False,
|
||||||
|
response_list=[
|
||||||
|
{"text": list_pleine},
|
||||||
|
{"text": list_vide},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
# on return m sans fermer le server qui simule la page
|
||||||
|
yield m
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def scraper() -> Scraper:
|
||||||
|
return Scraper()
|
||||||
|
|
||||||
|
|
||||||
|
def test_soup(scraper: Scraper):
|
||||||
|
vide = scraper.getsoup("")
|
||||||
|
poubelle = scraper.getsoup("poubelle")
|
||||||
|
contenu = scraper.getsoup("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.find("h1") is None
|
||||||
|
assert str(poubelle.find("h1")) == "<h1>POUBELLE</h1>"
|
||||||
|
assert str(contenu.find("h1")) == "<h1>MILLESIMA</h1>"
|
||||||
|
|
||||||
|
|
||||||
|
def test_appellation(scraper: Scraper):
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.appellation() is None
|
||||||
|
assert poubelle.appellation() is None
|
||||||
|
assert contenu.appellation() == "Madame-Loïk"
|
||||||
|
|
||||||
|
|
||||||
|
def test_fonctionprivee(scraper: Scraper):
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide._getattributes() is not None
|
||||||
|
assert vide._getattributes() == {}
|
||||||
|
assert vide._getcontent() is not None
|
||||||
|
assert vide._getcontent() == {"items": [], "attributes": {}}
|
||||||
|
assert poubelle._getattributes() is None
|
||||||
|
assert poubelle._getcontent() is None
|
||||||
|
assert contenu._getcontent() is not None
|
||||||
|
assert contenu._getattributes() is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_critiques(scraper: Scraper):
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.parker() is None
|
||||||
|
assert vide.robinson() is None
|
||||||
|
assert vide.suckling() is None
|
||||||
|
assert vide._getcritiques("test_ts") is None
|
||||||
|
assert poubelle.parker() is None
|
||||||
|
assert poubelle.robinson() is None
|
||||||
|
assert poubelle.suckling() is None
|
||||||
|
assert poubelle._getcritiques("test_ts") is None
|
||||||
|
assert contenu.parker() == "91"
|
||||||
|
assert contenu.robinson() == "17"
|
||||||
|
assert contenu.suckling() == "93.5"
|
||||||
|
assert contenu._getcritiques("test_ts") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_prix(scraper: Scraper):
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.prix() is None
|
||||||
|
assert poubelle.prix() is None
|
||||||
|
assert contenu.prix() == 842.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_informations(scraper: Scraper):
|
||||||
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert contenu.informations() == "Madame-Loïk,91,17,93.5,842.0"
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
assert vide.informations() == "None,None,None,None,None"
|
||||||
|
assert poubelle.informations() == "None,None,None,None,None"
|
||||||
|
|
||||||
|
|
||||||
|
def test_search(scraper: Scraper):
|
||||||
|
m = mock_open()
|
||||||
|
with patch("builtins.open", m):
|
||||||
|
scraper.getvins("wine.html", "fake_file.csv", True)
|
||||||
|
|
||||||
|
assert m().write.called
|
||||||
|
all_writes = "".join(call.args[0] for call in m().write.call_args_list)
|
||||||
|
assert "Madame-Loïk,91,17,93.5,842.0" in all_writes
|
||||||
Reference in New Issue
Block a user