[英]Scrape google snippet text in a certain language (English) using python and selenium
I was trying to scrape the snippet text from google search page and this solution worked well.我试图从谷歌搜索页面中抓取片段文本,这个解决方案效果很好。 The only issue I have now is that the text is in Bangla while I want it in English.
我现在唯一的问题是文本是孟加拉语,而我想要英语。
Here's what I've tried:这是我尝试过的:
options = webdriver.ChromeOptions()
options.add_argument('lang=en')
driver = webdriver.Chrome(executable_path=r'the\path\for\chromedriver.exe', options=options)
I've tried adding 'lang=en'
as an argument to ChromeOptions and pass it to webdriver.Chrome()
.我尝试将
'lang=en'
作为参数添加到 ChromeOptions 并将其传递给webdriver.Chrome()
。 That's all I could figure out but it's not working.这就是我能想到的,但它不起作用。
Here's the full code:这是完整的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
options.add_argument('lang=en')
driver = webdriver.Chrome(executable_path=r'C:\Users\camoh\AppData\Local\Programs\Python\Python38\chromedriver.exe', options=options)
driver.get('https://google.com/')
assert "Google" in driver.title
#wait = WebDriverWait(driver, 20)
#wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".gLFyf.gsfi")))
input_field = driver.find_element_by_css_selector(".gLFyf.gsfi")
input_field.send_keys("when barack obama born")
input_field.send_keys(Keys.RETURN)
#wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".Z0LcW.XcVN5d")))
result = driver.find_element_by_css_selector(".Z0LcW.XcVN5d").text
print(result)
driver.close()
driver.quit()
Here's the page when I run the code:这是我运行代码时的页面:
You can try with below code to add argument with preferred language:您可以尝试使用以下代码添加首选语言的参数:
from selenium.webdriver.chrome.options import Options as ChromeOptions #import library
options=webdriver.ChromeOptions() #create object of ChromeOptions
options.add_argument("--lang=en")
options.add_argument("--lang=en-US")#or you can use
Use -利用 -
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.