mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-29 03:23:47 +00:00
Compare commits
16 Commits
version_de
...
0f6eb856c6
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f6eb856c6 | |||
| d62145e250 | |||
| 829c303e78 | |||
| b584f9a301 | |||
| 547c7ec4c1 | |||
| 0aa765d6a0 | |||
| 8a357abe86 | |||
|
|
2f5af5aabf | ||
| a33b484dea | |||
| dd430b9861 | |||
| 011bb6a689 | |||
| 96dbaaaaf6 | |||
| ed86e588f7 | |||
|
|
0182bbbf20 | ||
| 9f1ff1ef7b | |||
|
|
8cae082344 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -205,3 +205,5 @@ cython_debug/
|
|||||||
marimo/_static/
|
marimo/_static/
|
||||||
marimo/_lsp/
|
marimo/_lsp/
|
||||||
__marimo__/
|
__marimo__/
|
||||||
|
|
||||||
|
*.csv
|
||||||
15
pyproject.toml
Normal file
15
pyproject.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[project]
|
||||||
|
name = "projet-millesima-s6"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"requests==2.32.5",
|
||||||
|
"beautifulsoup4==4.14.3",
|
||||||
|
"pandas==2.3.3",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
test = ["pytest==8.4.2", "requests-mock==1.12.1"]
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["setuptools"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
requests>=2.32.5
|
|
||||||
requests-mock>=1.12.1
|
|
||||||
beautifulsoup4>=4.14.3
|
|
||||||
Binary file not shown.
20
src/main.py
Executable file
20
src/main.py
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from os import getcwd
|
||||||
|
from os.path import normpath, join
|
||||||
|
from sys import argv
|
||||||
|
from pandas import read_csv, DataFrame
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
if len(argv) != 2:
|
||||||
|
raise ValueError(f"{argv[0]} <filename.csv>")
|
||||||
|
|
||||||
|
path: str = normpath(join(getcwd(), argv[1]))
|
||||||
|
db: DataFrame = read_csv(path)
|
||||||
|
print(db.all())
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERREUR: {e}")
|
||||||
110
main.py → src/scraper.py
Normal file → Executable file
110
main.py → src/scraper.py
Normal file → Executable file
@@ -1,12 +1,22 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from sys import argv
|
||||||
from typing import cast
|
from typing import cast
|
||||||
from requests import Response, Session
|
from requests import HTTPError, Response, Session
|
||||||
from bs4 import BeautifulSoup, Tag
|
from bs4 import BeautifulSoup, Tag
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from json import loads
|
from json import JSONDecodeError, loads
|
||||||
|
|
||||||
|
|
||||||
class _ScraperData:
|
class _ScraperData:
|
||||||
|
"""_summary_"""
|
||||||
|
|
||||||
def __init__(self, data: dict[str, object]) -> None:
|
def __init__(self, data: dict[str, object]) -> None:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data (dict[str, object]): _description_
|
||||||
|
"""
|
||||||
self._data: dict[str, object] = data
|
self._data: dict[str, object] = data
|
||||||
|
|
||||||
def _getcontent(self) -> dict[str, object] | None:
|
def _getcontent(self) -> dict[str, object] | None:
|
||||||
@@ -55,7 +65,6 @@ class _ScraperData:
|
|||||||
prix_calcule: float | None = None
|
prix_calcule: float | None = None
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -114,8 +123,8 @@ class _ScraperData:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
val = cast(str, app_dict.get("value")).rstrip("+").split("-")
|
val = cast(str, app_dict.get("value")).rstrip("+").split("-")
|
||||||
if len(val) > 1:
|
if len(val) > 1 and val[1] != "":
|
||||||
val[0] = str((int(val[0]) + int(val[1])) / 2)
|
val[0] = str(round((float(val[0]) + float(val[1])) / 2, 1))
|
||||||
|
|
||||||
return val[0]
|
return val[0]
|
||||||
return None
|
return None
|
||||||
@@ -165,6 +174,18 @@ class Scraper:
|
|||||||
# Très utile pour éviter de renvoyer toujours les mêmes handshake
|
# Très utile pour éviter de renvoyer toujours les mêmes handshake
|
||||||
# TCP et d'avoir toujours une connexion constante avec le server
|
# TCP et d'avoir toujours une connexion constante avec le server
|
||||||
self._session: Session = Session()
|
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
|
# Système de cache pour éviter de solliciter le serveur inutilement
|
||||||
self._latest_request: tuple[(str, Response)] | None = None
|
self._latest_request: tuple[(str, Response)] | None = None
|
||||||
self._latest_soups: OrderedDict[str, BeautifulSoup] = OrderedDict[
|
self._latest_soups: OrderedDict[str, BeautifulSoup] = OrderedDict[
|
||||||
@@ -185,7 +206,8 @@ class Scraper:
|
|||||||
HTTPError: Si le serveur renvoie un code d'erreur (4xx, 5xx).
|
HTTPError: Si le serveur renvoie un code d'erreur (4xx, 5xx).
|
||||||
"""
|
"""
|
||||||
target_url: str = self._url + subdir.lstrip("/")
|
target_url: str = self._url + subdir.lstrip("/")
|
||||||
response: Response = self._session.get(url=target_url, timeout=10)
|
# 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()
|
response.raise_for_status()
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -285,3 +307,79 @@ class Scraper:
|
|||||||
|
|
||||||
return _ScraperData(cast(dict[str, object], current_data))
|
return _ScraperData(cast(dict[str, object], current_data))
|
||||||
|
|
||||||
|
def _geturlproductslist(self, subdir: str) -> list[str] | None:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str): _description_
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: _description_
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
data: dict[str, object] = self.getjsondata(subdir).getdata()
|
||||||
|
|
||||||
|
for element in ["initialReduxState", "categ", "content"]:
|
||||||
|
data: dict[str, object] = cast(dict[str, object], data.get(element))
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return None
|
||||||
|
|
||||||
|
products: list[str] = cast(list[str], data.get("products"))
|
||||||
|
if isinstance(products, list):
|
||||||
|
return products
|
||||||
|
|
||||||
|
except (JSONDecodeError, HTTPError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def getvins(self, subdir: str, filename: str) -> None:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
subdir (str): _description_
|
||||||
|
filename (str): _description_
|
||||||
|
"""
|
||||||
|
with open(filename, "w") as f:
|
||||||
|
cache: set[str] = set[str]()
|
||||||
|
page = 0
|
||||||
|
_ = f.write("Appellation,Robert,Robinson,Suckling,Prix\n")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
page += 1
|
||||||
|
products_list: list[str] | None = \
|
||||||
|
self._geturlproductslist(f"{subdir}?page={page}")
|
||||||
|
|
||||||
|
if not products_list:
|
||||||
|
break
|
||||||
|
|
||||||
|
products_list_length = len(products_list)
|
||||||
|
for i, product in enumerate(products_list):
|
||||||
|
if not isinstance(product, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
|
link = product.get("seoKeyword")
|
||||||
|
|
||||||
|
if link and link not in cache:
|
||||||
|
try:
|
||||||
|
infos = self.getjsondata(link).informations()
|
||||||
|
_ = f.write(infos + "\n")
|
||||||
|
print(
|
||||||
|
f"page: {page} | {i + 1}/{products_list_length} {link}"
|
||||||
|
)
|
||||||
|
cache.add(link)
|
||||||
|
except (JSONDecodeError, HTTPError) as e:
|
||||||
|
print(f"Erreur sur le produit {link}: {e}")
|
||||||
|
f.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
if len(argv) != 2:
|
||||||
|
raise ValueError(f"{argv[0]} <sous-url>")
|
||||||
|
scraper: Scraper = Scraper()
|
||||||
|
scraper.getvins(argv[1], "donnee.csv")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERREUR: {e}")
|
||||||
0
tests/test_main.py
Normal file
0
tests/test_main.py
Normal file
@@ -1,8 +1,8 @@
|
|||||||
from json import dumps
|
from json import dumps
|
||||||
from bs4 import Tag
|
from unittest.mock import patch, mock_open
|
||||||
import pytest
|
import pytest
|
||||||
from requests_mock import Mocker
|
from requests_mock import Mocker
|
||||||
from main import Scraper
|
from scraper import Scraper
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@@ -71,10 +71,10 @@ def mock_site():
|
|||||||
"_id": "J4131/22/C/CC/6-11652",
|
"_id": "J4131/22/C/CC/6-11652",
|
||||||
"partnumber": "J4131/22/C/CC/6",
|
"partnumber": "J4131/22/C/CC/6",
|
||||||
"taxRate": "H",
|
"taxRate": "H",
|
||||||
"listPrice": 390,
|
"listPrice": 842,
|
||||||
"offerPrice": 390,
|
"offerPrice": 842,
|
||||||
"seoKeyword": "nino-negri-5-stelle-sfursat-2022-c-cc-6.html",
|
"seoKeyword": "vin-de-charazade1867.html",
|
||||||
"shortdesc": "Un carton de 6 Bouteilles (75cl)",
|
"shortdesc": "Une bouteille du meilleur vin du monde?",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
"promotion_o_n": {
|
"promotion_o_n": {
|
||||||
"valueId": "0",
|
"valueId": "0",
|
||||||
@@ -101,9 +101,9 @@ def mock_site():
|
|||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
"nbunit": {
|
"nbunit": {
|
||||||
"valueId": "6",
|
"valueId": "1",
|
||||||
"name": "nbunit",
|
"name": "nbunit",
|
||||||
"value": "6",
|
"value": "1",
|
||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -120,14 +120,14 @@ def mock_site():
|
|||||||
"appellation": {
|
"appellation": {
|
||||||
"valueId": "433",
|
"valueId": "433",
|
||||||
"name": "Appellation",
|
"name": "Appellation",
|
||||||
"value": "Sforzato di Valtellina",
|
"value": "Madame-Loïk",
|
||||||
"url": "sforzato-di-valtellina.html",
|
"url": "Madame-loik.html",
|
||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
"groupIdentifier": "appellation_433",
|
"groupIdentifier": "appellation_433",
|
||||||
},
|
},
|
||||||
"note_rp": {
|
"note_rp": {
|
||||||
"valueId": "91",
|
"valueId": "91",
|
||||||
"name": "Parker",
|
"name": "Peter Parker",
|
||||||
"value": "91",
|
"value": "91",
|
||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
@@ -138,8 +138,8 @@ def mock_site():
|
|||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
"note_js": {
|
"note_js": {
|
||||||
"valueId": "93-94",
|
"valueId": "93-94.5",
|
||||||
"name": "J. Suckling",
|
"name": "J. cherazade",
|
||||||
"value": "93-94",
|
"value": "93-94",
|
||||||
"isSpirit": False,
|
"isSpirit": False,
|
||||||
},
|
},
|
||||||
@@ -166,6 +166,79 @@ def mock_site():
|
|||||||
text=html_product,
|
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
|
# on return m sans fermer le server qui simule la page
|
||||||
yield m
|
yield m
|
||||||
|
|
||||||
@@ -190,7 +263,7 @@ def test_appellation(scraper: Scraper):
|
|||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert vide.appellation() is None
|
assert vide.appellation() is None
|
||||||
assert poubelle.appellation() is None
|
assert poubelle.appellation() is None
|
||||||
assert contenu.appellation() == "Sforzato di Valtellina"
|
assert contenu.appellation() == "Madame-Loïk"
|
||||||
|
|
||||||
|
|
||||||
def test_fonctionprivee(scraper: Scraper):
|
def test_fonctionprivee(scraper: Scraper):
|
||||||
@@ -207,7 +280,6 @@ def test_fonctionprivee(scraper: Scraper):
|
|||||||
assert contenu._getattributes() is not None
|
assert contenu._getattributes() is not None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_critiques(scraper: Scraper):
|
def test_critiques(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
@@ -232,13 +304,23 @@ def test_prix(scraper: Scraper):
|
|||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert vide.prix() is None
|
assert vide.prix() is None
|
||||||
assert poubelle.prix() is None
|
assert poubelle.prix() is None
|
||||||
assert contenu.prix() == 65.0
|
assert contenu.prix() == 842.0
|
||||||
|
|
||||||
|
|
||||||
def test_informations(scraper: Scraper):
|
def test_informations(scraper: Scraper):
|
||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert contenu.informations() == "Sforzato di Valtellina,91,17,93.5,65.0"
|
assert contenu.informations() == "Madame-Loïk,91,17,93.5,842.0"
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
assert vide.informations() == "None,None,None,None,None"
|
assert vide.informations() == "None,None,None,None,None"
|
||||||
assert poubelle.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")
|
||||||
|
|
||||||
|
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