-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsueperScraper.py
More file actions
76 lines (46 loc) · 1.79 KB
/
sueperScraper.py
File metadata and controls
76 lines (46 loc) · 1.79 KB
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
import requests
from bs4 import BeautifulSoup
import csv
import json
from collections import Counter
mots_cles = ["life", "truth", "python"]
tags_recherches = ["inspirational", "wisdom"]
fichier_csv = open("citations_filtrees.csv", "w", newline='', encoding="utf-8")
writer = csv.writer(fichier_csv)
writer.writerow(["Citation", "Auteur", "Tags"])
citations_json = []
auteurs_counter = Counter()
page = 1
while True:
url = f"https://quotes.toscrape.com/page/{page}/"
print(f" Page {page} : {url}")
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
quotes = soup.find_all("div", class_="quote")
if not quotes:
print(" Fin des pages.")
break
for quote in quotes:
texte = quote.find("span", class_="text").text
auteur = quote.find("small", class_="author").text
tags = [tag.text.lower() for tag in quote.find_all("a", class_="tag")]
contient_mot = any(mot in texte.lower() for mot in mots_cles)
contient_tag = any(tag in tags for tag in tags_recherches)
if contient_mot or contient_tag:
writer.writerow([texte, auteur, ", ".join(tags)])
citations_json.append({
"texte": texte,
"auteur": auteur,
"tags": tags
})
auteurs_counter[auteur] += 1
page += 1
fichier_csv.close()
with open("citations_filtrees.json", "w", encoding="utf-8") as f:
json.dump(citations_json, f, ensure_ascii=False, indent=4)
print("\n Classement des auteurs les plus cités :")
for auteur, nombre in auteurs_counter.most_common():
print(f"{auteur} : {nombre} citation(s)")
print("\n✅ Scraping terminé ! Données enregistrées dans:")
print("➡ citations_filtrees.csv")
print("➡ citations_filtrees.json")