Python Forum
scraping webiste using pandas and googletranslation dont compile on the terminal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scraping webiste using pandas and googletranslation dont compile on the terminal
#1
Hi Team

I need some help with my script, i am trying to scrap the website but when i try to use data frame to translate the text from the website its not running but its crashing. Here is my terminal output below...

// Code to execute this..

from googletrans import Translator
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
import pandas as pd
import urllib.request as urllib2
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument("window-size=1920,1080")
driver = webdriver.Chrome(options=chrome_options)
url = "https://www.classcentral.com/collection/top-free-online-courses"
driver.get(url)

translator = Translator()

try:
    while True:
        # wait until button is clickable
        WebDriverWait(driver, 1).until(
            expected_conditions.element_to_be_clickable((By.XPATH, "//button[@data-name='LOAD_MORE']"))
        ).click()
        time.sleep(0.5)
except Exception as e:
    pass

all_courses = driver.find_element(by=By.CLASS_NAME, value='catalog-grid__results')
courses = all_courses.find_elements(by=By.CSS_SELECTOR, value='[class="color-charcoal course-name"]')

df = pd.DataFrame([[course.text, course.get_attribute('href')] for course in courses],
                  columns=['Title (eng)', 'Link'])

df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translator.translate(x, dest='hi').text)

print(df)
// output from the terminal
Output:
File "C:\Users\Zux\PycharmProjects\ClassCentral\ClassCentral\Lib\site-packages\googletrans\client.py", line 182, in translate data = self._translate(text, dest, src, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Zux\PycharmProjects\ClassCentral\ClassCentral\Lib\site-packages\googletrans\client.py", line 78, in _translate token = self.token_acquirer.do(text) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Zux\PycharmProjects\ClassCentral\ClassCentral\Lib\site-packages\googletrans\gtoken.py", line 194, in do self._update() File "C:\Users\Zux\PycharmProjects\ClassCentral\ClassCentral\Lib\site-packages\googletrans\gtoken.py", line 62, in _update code = self.RE_TKK.search(r.text).group(1).replace('var ', '') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'group'
Reply
#2
The problem is with googletrans
Do this.
pip uninstall googletrans
Then.
pip install googletrans==3.1.0a0
Test of your code.
df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translator.translate(x, dest='hi').text)
No error and now is translated to Hindi.
>>> df['Title (hin)']
0             मेडिकल पैरासिटोलॉजी | मेडिकल पैरासिटोलॉजी
1     मेडिकल रिसर्च को समझना: आपका फेसबुक फ्रेंड गलत है
2     पायथन में इंटरएक्टिव प्रोग्रामिंग का परिचय (भा...
3                                             पर्वत 101
4                          सभी के लिए क्वांटम यांत्रिकी
5                                    डिमेंशिया को समझना
6             आधुनिक और समकालीन अमेरिकी कविता ("मॉडपो")
7     सीखना कैसे सीखें: कठिन विषयों में महारत हासिल ...
8                              एक दिमागी जीवन बनाए रखना
9                   मल्टीपल स्केलेरोसिस (एमएस) को समझना
10                                असामान्य ज्ञान शिक्षण
11                       विपणन अभिनव उत्पादों और सेवाओं
12    एक पेशेवर की तरह सीखें: किसी भी चीज़ में बेहतर...
13                 जीव विज्ञान का परिचय - जीवन का रहस्य
14                 भलाई और शिखर प्रदर्शन के लिए सचेतनता
Name: Title (hin), dtype: object
Reply
#3
(Mar-06-2023, 07:23 PM)snippsat Wrote: The problem is with googletrans
Do this.
pip uninstall googletrans
Then.
pip install googletrans==3.1.0a0
Test of your code.
df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translator.translate(x, dest='hi').text)
No error and now is translated to Hindi.
>>> df['Title (hin)']
0             मेडिकल पैरासिटोलॉजी | मेडिकल पैरासिटोलॉजी
1     मेडिकल रिसर्च को समझना: आपका फेसबुक फ्रेंड गलत है
2     पायथन में इंटरएक्टिव प्रोग्रामिंग का परिचय (भा...
3                                             पर्वत 101
4                          सभी के लिए क्वांटम यांत्रिकी
5                                    डिमेंशिया को समझना
6             आधुनिक और समकालीन अमेरिकी कविता ("मॉडपो")
7     सीखना कैसे सीखें: कठिन विषयों में महारत हासिल ...
8                              एक दिमागी जीवन बनाए रखना
9                   मल्टीपल स्केलेरोसिस (एमएस) को समझना
10                                असामान्य ज्ञान शिक्षण
11                       विपणन अभिनव उत्पादों और सेवाओं
12    एक पेशेवर की तरह सीखें: किसी भी चीज़ में बेहतर...
13                 जीव विज्ञान का परिचय - जीवन का रहस्य
14                 भलाई और शिखर प्रदर्शन के लिए सचेतनता
Name: Title (hin), dtype: object

Thank you so much, you are lifesaver, came to the right forum. Its now running as way expected with no issues at all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I dont know why my function won't work? MehHz2526 3 1,210 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,298 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  why I dont get any output from this code William369 2 1,146 Jun-23-2022, 09:18 PM
Last Post: William369
  [split] import PIL dont work vedansh 1 2,104 Mar-29-2020, 10:00 AM
Last Post: Larz60+
  import PIL dont work rodink 14 13,001 Feb-22-2020, 08:48 PM
Last Post: snippsat
  I dont understand bytes in python. blackknite 3 4,054 Oct-02-2019, 07:39 PM
Last Post: Gribouillis
Exclamation Get syntax error. Dont understand why beLIEve 21 14,719 Aug-25-2019, 02:28 PM
Last Post: ThomasL
  Tests are not running .Dont why shankar 3 6,344 Sep-20-2018, 07:06 AM
Last Post: shankar
  Code dont work FrankWhite 11 6,308 Mar-31-2018, 09:07 PM
Last Post: FrankWhite
  Dont know How papampi 2 3,182 Oct-23-2017, 04:35 PM
Last Post: papampi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020