search-duckduck
This commit is contained in:
+38
-18
@@ -1,32 +1,32 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
# Vérifie et installe bs4 si nécessaire
|
||||
try:
|
||||
from bs4 import BeautifulSoup
|
||||
except ImportError:
|
||||
print("bs4 non trouvé, installation en cours...")
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "beautifulsoup4"])
|
||||
from bs4 import BeautifulSoup
|
||||
# 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")
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
import requests
|
||||
import argparse
|
||||
|
||||
|
||||
def recherche_duckduckgo(query, n=5):
|
||||
url = "https://html.duckduckgo.com/html/"
|
||||
params = {"q": query}
|
||||
headers = {"User-Agent": "Mozilla/5.0"}
|
||||
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0"
|
||||
}
|
||||
|
||||
response = requests.post(url, data=params, headers=headers)
|
||||
response = requests.post(url, data={"q": query}, headers=headers)
|
||||
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")
|
||||
@@ -35,18 +35,38 @@ def recherche_duckduckgo(query, n=5):
|
||||
return results
|
||||
|
||||
|
||||
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}]"
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Recherche DuckDuckGo simple")
|
||||
parser = argparse.ArgumentParser(description="Recherche + extraction de texte")
|
||||
parser.add_argument("query", type=str, help="Terme de recherche")
|
||||
parser.add_argument("-n", "--number", type=int, default=5, help="Nombre de résultats")
|
||||
parser.add_argument("-n", "--number", type=int, default=3, help="Nombre de résultats")
|
||||
parser.add_argument("--chars", type=int, default=300, help="Nombre de caractères à extraire")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
results = recherche_duckduckgo(args.query, args.number)
|
||||
|
||||
for i, (title, link) in enumerate(results, 1):
|
||||
print(f"{i}. {title}")
|
||||
print(f" {link}\n")
|
||||
print(f"\n{i}. {title}")
|
||||
print(f"URL: {link}")
|
||||
|
||||
snippet = extract_text(link, args.chars)
|
||||
print(f"Extrait: {snippet}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user