#话不多说上代码...效率慢的出奇QAQ
#突然发现有api,重构代码ing
from selenium import webdriver
driver = webdriver.Chrome()
import string
eng_word = list(string.ascii_lowercase)
def request_and_return(request_url):
driver.get(request_url)
try:
translate = driver.find_element_by_xpath('//*[@id="main-outer"]/div/div/div/div[2]/div[1]/div[2]/div/div/div[1]/p[2]/span').text
except Exception as e:
print(driver.page_source)
pass
return translate
def main_function(word):
words_list = list(word)
if words_list[0] in eng_word:
request_url = "http://fanyi.baidu.com/#en/zh/" + word
translate = request_and_return(request_url)
print(translate)
else:
request_url = "http://fanyi.baidu.com/#zh/en/" + word
translate = request_and_return(request_url)
print(translate)
while True:
word = input("输入你想翻译的内容")
if word == 'q':
break
else:
main_function(word)