Files
DuckGoSearch/DuckDuckGo.py
T
2026-03-02 15:42:07 +07:00

42 lines
862 B
Python

import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
def search_duckduckgo(query):
session = requests.Session()
session.headers.update({
"User-Agent": "Mozilla/5.0"
})
response = session.post(
"https://html.duckduckgo.com/html/",
data={
"q": query,
"kl": "us-en",
"df": "w"
}
)
soup = BeautifulSoup(response.text, "html.parser")
results = []
for a in soup.find_all("a", class_="result__a"):
title = a.get_text(strip=True)
url = a.get("href")
if not url:
continue
domain = urlparse(url).netloc.lower()
# Bỏ .vn
if domain.endswith(".vn"):
continue
results.append({
"title": title,
"url": url
})
return results