2026-03-29 13:58:18 +00:00
|
|
|
#!/usr/bin/env python
|
2026-03-29 14:04:59 +00:00
|
|
|
|
2026-03-29 14:01:35 +00:00
|
|
|
import sys
|
|
|
|
|
import subprocess
|
|
|
|
|
|
2026-03-29 14:04:59 +00:00
|
|
|
# Vérifie et installe les dépendances si nécessaire
|
|
|
|
|
def ensure_package(pkg):
|
|
|
|
|
try:
|
|
|
|
|
__import__(pkg)
|
|
|
|
|
except ImportError:
|
|
|
|
|
print(f"{pkg} non trouvé, installation...")
|
|
|
|
|
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
|
|
|
|
|
|
|
|
|
|
ensure_package("bs4")
|
|
|
|
|
ensure_package("requests")
|
2026-03-29 13:58:18 +00:00
|
|
|
|
2026-03-29 14:04:59 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2026-03-29 13:58:18 +00:00
|
|
|
import requests
|
2026-03-29 14:01:35 +00:00
|
|
|
import argparse
|
|
|
|
|
|
2026-03-29 13:58:18 +00:00
|
|
|
|
|
|
|
|
def recherche_duckduckgo(query, n=5):
|
|
|
|
|
url = "https://html.duckduckgo.com/html/"
|
2026-03-29 14:04:59 +00:00
|
|
|
headers = {"User-Agent": "Mozilla/5.0"}
|
2026-03-29 13:58:18 +00:00
|
|
|
|
2026-03-29 14:04:59 +00:00
|
|
|
response = requests.post(url, data={"q": query}, headers=headers)
|
2026-03-29 13:58:18 +00:00
|
|
|
soup = BeautifulSoup(response.text, "html.parser")
|
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
for result in soup.find_all("a", class_="result__a", limit=n):
|
|
|
|
|
title = result.get_text()
|
|
|
|
|
link = result.get("href")
|
|
|
|
|
results.append((title, link))
|
|
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
2026-03-29 14:04:59 +00:00
|
|
|
def extract_text(url, max_chars=300):
|
|
|
|
|
try:
|
|
|
|
|
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
|
|
response = requests.get(url, headers=headers, timeout=5)
|
|
|
|
|
|
|
|
|
|
soup = BeautifulSoup(response.text, "html.parser")
|
|
|
|
|
|
|
|
|
|
# Récupère le texte brut
|
|
|
|
|
text = soup.get_text(separator=" ", strip=True)
|
|
|
|
|
|
|
|
|
|
return text[:max_chars] + "..."
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return f"[Erreur lors de l'accès au site: {e}]"
|
|
|
|
|
|
|
|
|
|
|
2026-03-29 14:01:35 +00:00
|
|
|
def main():
|
2026-03-29 14:04:59 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Recherche + extraction de texte")
|
2026-03-29 14:01:35 +00:00
|
|
|
parser.add_argument("query", type=str, help="Terme de recherche")
|
2026-03-29 14:40:56 +00:00
|
|
|
parser.add_argument("-n", "--number", type=int, default=5, help="Nombre de résultats")
|
|
|
|
|
parser.add_argument("--chars", type=int, default=1000, help="Nombre de caractères à extraire")
|
2026-03-29 14:01:35 +00:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2026-03-29 13:58:18 +00:00
|
|
|
|
2026-03-29 14:01:35 +00:00
|
|
|
results = recherche_duckduckgo(args.query, args.number)
|
2026-03-29 13:58:18 +00:00
|
|
|
|
|
|
|
|
for i, (title, link) in enumerate(results, 1):
|
2026-03-29 14:04:59 +00:00
|
|
|
print(f"\n{i}. {title}")
|
|
|
|
|
print(f"URL: {link}")
|
|
|
|
|
|
|
|
|
|
snippet = extract_text(link, args.chars)
|
|
|
|
|
print(f"Extrait: {snippet}")
|
2026-03-29 14:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|