鱼C论坛

 找回密码
 立即注册
查看: 2757|回复: 17

[已解决]百度翻译

[复制链接]
发表于 2018-10-24 13:26:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看别人发了一个百度翻译的代码:
  1. from selenium import webdriver
  2. import time

  3. options = webdriver.ChromeOptions()
  4. options.add_argument('--headless')
  5. options.add_argument('--disable-gpu')
  6. browser = webdriver.Chrome(chrome_options = options)
  7. browser.get('https://fanyi.baidu.com/#auto/zh/')
  8. input_area = browser.find_element_by_css_selector('#baidu_translate_input')

  9. content = input('请输入你需要翻译的内容:')
  10. input_area.send_keys(content)
  11. time.sleep(2)
  12. output_area = browser.find_element_by_css_selector('#main-outer > div > div > \
  13. div.translate-wrap > div.translateio > div.translate-main.clearfix > \
  14. div.trans-right > div > div > div.output-bd > \
  15. p.ordinary-output.target-output.clearfix > span')
  16. print('翻译结果:',output_area.text)
  17. browser.close()
复制代码

执行结果:
  1. Traceback (most recent call last):
  2.   File "D:\Python\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
  3.     stdin=PIPE)
  4.   File "D:\Python\lib\subprocess.py", line 709, in __init__
  5.     restore_signals, start_new_session)
  6.   File "D:\Python\lib\subprocess.py", line 997, in _execute_child
  7.     startupinfo)
  8. FileNotFoundError: [WinError 2] 系统找不到指定的文件。

  9. During handling of the above exception, another exception occurred:

  10. Traceback (most recent call last):
  11.   File "C:\Users\admin\Desktop\hello.py", line 7, in <module>
  12.     browser = webdriver.Chrome(chrome_options = options)
  13.   File "D:\Python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
  14.     self.service.start()
  15.   File "D:\Python\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
  16.     os.path.basename(self.path), self.start_error_message)
  17. selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
复制代码

求教
最佳答案
2018-10-24 18:23:53
https://fishc.com.cn/thread-121621-1-1.html你按这个帖子的镜像去下载chromedriver,下载完放到scripts文件夹下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-24 15:40:34 | 显示全部楼层
要下对应浏览器的版本的驱动,并把它放在 Python 安装目录里,selenium 才会正常工作
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-24 18:02:45 | 显示全部楼层
下载谷歌浏览器,下载对应的chromedriver,那不是我的代码吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-24 18:21:39 | 显示全部楼层
塔利班 发表于 2018-10-24 18:02
下载谷歌浏览器,下载对应的chromedriver,那不是我的代码吗

确实,我最近在搞百度翻译,看到你这代码直接拿来用了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-24 18:23:53 | 显示全部楼层    本楼为最佳答案   
https://fishc.com.cn/thread-121621-1-1.html你按这个帖子的镜像去下载chromedriver,下载完放到scripts文件夹下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-24 18:41:07 | 显示全部楼层
对了,对于谷歌内核的浏览器,代码有效吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-25 17:23:43 | 显示全部楼层
  1. Traceback (most recent call last):
  2.   File "C:\Users\admin\Desktop\hello.py", line 7, in <module>
  3.     browser = webdriver.Chrome(chrome_options = options)
  4.   File "D:\Python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
  5.     desired_capabilities=desired_capabilities)
  6.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
  7.     self.start_session(capabilities, browser_profile)
  8.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
  9.     response = self.execute(Command.NEW_SESSION, parameters)
  10.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
  11.     self.error_handler.check_response(response)
  12.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  13.     raise exception_class(message, screen, stacktrace)
  14. selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
  15.   (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64)
复制代码

又怎么了(谷歌驱动版本:2.29,2345浏览器谷歌内核:56.0.2924.90)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-25 17:32:57 | 显示全部楼层
你没下对版本
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-25 17:34:53 | 显示全部楼层
下2。39的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-26 20:26:27 | 显示全部楼层
还是不行……因此换IE了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-26 21:27:07 | 显示全部楼层
你下了那个,然后360软件管家按个谷歌浏览器就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-28 12:59:21 | 显示全部楼层
代码改了一下:
  1. from selenium import webdriver
  2. import time

  3. content = input('请输入你需要翻译的内容:')
  4. options = webdriver.IeOptions()
  5. options.add_argument('--headless')
  6. options.add_argument('--disable-gpu')
  7. browser = webdriver.Ie(options = options)
  8. browser.get('https://fanyi.baidu.com')
  9. input_area = browser.find_element_by_css_selector('#baidu_translate_input')

  10. input_area.send_keys(content)
  11. time.sleep(2)
  12. output_area = browser.find_element_by_css_selector('#main-outer > div > div > \
  13. div.translate-wrap > div.translateio > div.translate-main.clearfix > \
  14. div.trans-right > div > div > div.output-bd > \
  15. p.ordinary-output.target-output.clearfix > span')
  16. print('翻译结果:',output_area.text)
  17. browser.close()
复制代码

结果:
  1. 请输入你需要翻译的内容:hello
  2. Traceback (most recent call last):
  3.   File "C:\Users\admin\Desktop\hello.py", line 10, in <module>
  4.     input_area = browser.find_element_by_css_selector('#baidu_translate_input')
  5.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector
  6.     return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  7.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
  8.     'value': value})['value']
  9.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
  10.     self.error_handler.check_response(response)
  11.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  12.     raise exception_class(message, screen, stacktrace)
  13. selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == #baidu_translate_input
复制代码

IE能正常启动,这是什么情况
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-31 12:03:27 | 显示全部楼层
@塔利班
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-31 12:51:12 | 显示全部楼层
你这么好像@不出来我,我也没有提醒,当网页没有加载完毕(你的网速慢的时候可能控件没有加载出来),
需要until那个什么函数,这里用sleep只是人为控制时间然后读取,可能你的网速比较慢,未实现控件加载
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-31 14:11:26 | 显示全部楼层
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC

  5. driver = webdriver.Firefox()
  6. driver.get("http://somedomain/url_that_delays_loading")
  7. try:
  8.     element = WebDriverWait(driver, 10).until(
  9.         EC.presence_of_element_located((By.ID, "myDynamicElement"))
  10.     )
  11. finally:
  12.     driver.quit()
复制代码

这个是selenium 官方的waits文档
参照这个你把ID换成CSS选择器,对应变量名设置下,我没有按IE得驱动,你按文档改下就行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-1 12:09:17 | 显示全部楼层
IE加载慢,超时了……代码仍在改进中
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-1 13:31:53 | 显示全部楼层
  1. 请输入你需要翻译的内容:hello
  2. Traceback (most recent call last):
  3.   File "C:\Users\admin\Desktop\hello.py", line 13, in <module>
  4.     EC.presence_of_element_located((By.ID, "#baidu_translate_input"))
  5.   File "D:\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
  6.     raise TimeoutException(message, screen, stacktrace)
  7. selenium.common.exceptions.TimeoutException: Message:
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-12 06:33:33 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 00:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表