ترجمة
import requests
from bs4 import BeautifulSoup
def translate_ar_to_en(text):
"""Translates text from Arabic to English.
Args:
text: The text to translate.
Returns:
The translated text.
"""
# Set the HTTP headers.
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
"Accept-Language": "en-US,en;q=0.9",
}
# Make the HTTP request.
response = requests.get(
"https://translate.google.com/translate_a/single?client=t&sl=ar&tl=en&dt=t&q=" + text,
headers=headers,
)
# Parse the HTTP response.
soup = BeautifulSoup(response.content, "html.parser")
# Get the translated text.
translated_text = soup.find("span", {"class": "tlid-translation"}).text
# Return the translated text.
return translated_text
تعليقات
إرسال تعليق