-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-translation.py
More file actions
35 lines (27 loc) · 1.06 KB
/
Copy pathgoogle-translation.py
File metadata and controls
35 lines (27 loc) · 1.06 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
import requests
from bs4 import BeautifulSoup
def scrape_google_translate(text, source_language, target_language):
# Google Translate URL
url = "https://translate.google.com/m"
# Prepare the payload with the query parameters
payload = {
'hl': target_language,
'q': text,
'sl': source_language,
'tl': target_language,
}
# Make the request to Google Translate
response = requests.get(url, params=payload)
# Parse the response with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the div with the 'swap-container' class
swap_container = soup.find('div', class_='result-container')
# Extract the text or other desired parts from the swap_container
if swap_container:
translated_text = swap_container.get_text() # or any other method depending on what you need
else:
translated_text = "The result-container div was not found."
return translated_text
# Example usage:
translated = scrape_google_translate("Hello, world!", "en", "fr")
print(translated)