编程的过程中,不可避免的会遇到不认识的单词,这时候就需要使用谷歌翻译来解决问题了,但是用google翻译查询,查过的单词还要手动保存,不然就没了,十分麻烦,于是决定自己用爬虫做个工具玩玩.
思路是:
1.模拟访问谷歌翻译,输入需要翻译的文字
2.选择要翻译成的语言
3.将翻译结果爬取下来再保存
1.创建浏览器
class Trans(object):
def __init__(self,timeout= None):
self.timout = timeout
self.chrome_options = Options()
self.chrome_options.add_argument("--headless")
self.browser = webdriver.Chrome(chrome_options=self.chrome_options)
#有界面模式
# self.browser = webdriver.Chrome()
2.访问谷歌翻译
def get_trans(self):
self.browser.get("https://translate.google.cn/")
words = self.input_words()
self.browser.find_element_by_id("source").send_keys(words)
sleep(1)
self.working(words)