我正在尝试抓取世界各地的多个网页。所以,我想使用谷歌翻译扩展程序翻译网站,然后使用硒抓取页面。
我做了一些研究并想出了如何在运行 selenium 时添加扩展。
1)下载谷歌翻译扩展
但我不知道如何自动执行扩展(默认情况下,它什么都不做)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
option = webdriver.ChromeOptions()
option.add_extension('./translate.crx')
driver = webdriver.Chrome(executable_path = "./chromedriver", chrome_options = option)
driver.get("naver.com")
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
''' @@@@ Here I want something like@@@@
driver.execute_extension("translate this page")
'''
print driver.find_element_by_tag_name("body").text
driver.quit()
此外,我发现该扩展程序不会翻译原始 HTML,因此我可能必须使用不同的方法进行爬网。(也许通过 ctrl-a、ctrl-c、ctrl-v 代替 by_tag_name("body"))
你能给我任何指示吗?
提前致谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句