hi team
I have a working script from python, but its not translating my english text into Hindi and used pandas as data frame to count number of courses from the website and does give me list of courses available. But now the problem my translating these courses from English to Hindi is translatexx0000. I need some help here could be doing something from the data frame in my lamba expression to translate these text into Hindi.
// python script
Python
import translate
from google_trans 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 = "./....classcentral.com/collection/top-free-online-courses"//i cant put url links dont have enough reputation
driver.get(url)
#google translating languages
#translate = 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('')] for course in courses],
columns=['Title (eng)', 'Link'])
df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translate.Translator(x, dest='hi'))// problem is here and need some help
print(df)
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 3, 2023, 6:06 PM EST
Hello,
See the updated code snippet,I have modified it and it should translate the course titles correctly:
from google_trans 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 = "./....classcentral.com/collection/top-free-online-courses"//i cant put url links dont have enough reputation driver.get(url) try: while True: 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('')] for course in courses], columns=['Title (eng)', 'Link']) # Create a translator object translator = Translator() # Translate each course title to Hindi df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translator.translate(x, dest='hi').text) print(df)This should create a new column in the DataFrame called Title (hin) that contains the Hindi translation of the course titles.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 4, 2023, 10:30 AM EST
Yes, I think the error is because you are passing an empty string as an argument to the get_attribute method. To fix this, you need to pass the attribute name as the argument. In this case, it should be href. Try the code below:
from google_trans 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 = "./....classcentral.com/collection/top-free-online-courses" # replace with actual URL driver.get(url) try: while True: 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']) # Create a translator object translator = Translator() # Translate each course title to Hindi df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translator.translate(x, dest='hi').text) print(df)Make sure to replace the url variable with the actual URL of the page you want to scrape.
Guest UserRank #877 · 2.5K reputationPosted Mar 4, 2023, 4:28 AM EST
Hi Tuhin
Do you mind sharing your output here as a solution, because what you mentioned now here tried and this compiles this screen code below. I have all the necessary libraries used from the program but i kept getting this exception all the time. What is best option to fix this problem...?
Traceback (most recent call last): File "C:\Users\Zux\PycharmProjects\ClassCentral\main.py", line 37, in df['Title (hin)'] = df['Title (eng)'].apply(lambda x: translator.translate(x, dest='hi').text) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Zux\PycharmProjects\ClassCentral\ClassCentral\Lib\site-packages\pandas\core\series.py", line 4771, in apply return SeriesApply(self, func, convert_dtype, args, kwargs).apply() from google_trans 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) #google translating languages 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)Guest UserRank #877 · 2.5K reputationPosted Mar 4, 2023, 2:58 PM EST
Hi Tuhin
Have a look at the code that is now inserted its the same i posted today and im stil experiencing the same issue. Please test this code again on your side and share the compiled output. I might be doing something wrong here.
// latest code with URL link included but still experience same result no change at all.
from google_trans 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) 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"]') #google translating languages translator = Translator() 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)