Reputation: 21
As a Challenge to my self I'm trying to scrape translations from google translate. I download the html file by a browser simulated request and then using beautifulSoupe 4 I try finding the translation in the html code. The problem is that although I can find what I want in Chrome WebDev Mode I cant do such thing in Python or using another program such as noteblock. Here is my code:
while Translating==True:
text=input(("Values to translate:"))
#CREATING URL TO SCRAPE
translator_url_with_translation_embebed= translator_url+ EncodeTexttoURL(text)
#REQUESTING PAGE WITH FAKE HEADERS (avoiding no browser detection)
url = translator_url_with_translation_embebed
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response= requests.get(url, headers=headers)
print(response.content)
#ANALISING RESPONSE FOR FURTHER USE
sopa = BeautifulSoup(response.content)
translation_location=sopa.find_all('span', {'class' : 'tlid-translation translation'})
print("TRANSLATED LOCATIOON")
print(translation_location
` translation_location outputs an empty list.
Browser Dev mode showing translation in html code:
How can i find them? Are the translations elsewhere? Did google sent a modified version of their website to fool me?!
Thanks for the help
Upvotes: 2
Views: 5042
Reputation: 1053
py -m pip install "googletrans"
py -m pip install googletrans==4.0.0rc1
py -m pip install beautifulsoup4
from bs4 import BeautifulSoup
from bs4.formatter import HTMLFormatter
from googletrans import Translator
import requests
translator = Translator()
Check the complete Python code here:
OR HERE:
https://neculaifantanaru.com/en/example-google-translate-api-key-python-code-beautifulsoup.html
Upvotes: 0
Reputation: 19154
translated word are coming from XMLHttpRequest and beautifulsoup cannot handle this. duplicate the request (complicated) or use Selenium. But I think it easier to just use googletrans library.
$ pip install googletrans
then your code
from googletrans import Translator
while Translating==True:
translator = Translator()
text=input(("Values to translate:"))
translation = translator.translate(text, dest='en')
print(translation.text)
Upvotes: 5