Google dịch Python sang javascript

Google Dịch là một dịch vụ dịch thuật đa ngôn ngữ miễn phí, dựa trên dịch máy thống kê và thần kinh, được phát triển bởi Google. Nó được sử dụng rộng rãi để dịch các trang web hoặc trang web hoàn chỉnh từ ngôn ngữ này sang ngôn ngữ khác.  

Chúng tôi sẽ tạo một ứng dụng đầu cuối python sẽ lấy ngôn ngữ nguồn, ngôn ngữ đích, một cụm từ để dịch và trả về văn bản đã dịch. Chúng tôi sẽ triển khai các kỹ thuật kiểm tra đơn vị và quét web với selen trong python. Quét web là một khái niệm thu thập dữ liệu cần thiết từ một trang web. Selenium là một thư viện cấp ngành được sử dụng để quét web và kiểm tra đơn vị của nhiều phần mềm khác nhau. Như một điều kiện tiên quyết, chúng tôi sẽ yêu cầu các công cụ sau được cài đặt trong hệ thống của chúng tôi

  • Trăn 3. x. Một phiên bản của trăn 3. 0 trở lên nên được cài đặt
  • thư viện selen. Một thư viện python cần thiết để loại bỏ các trang web. Sao chép câu lệnh sau để cài đặt Selenium trên hệ thống của bạn
    Cài đặt. python3 -m pip cài đặt Selenium
  • webdriver. Phiên bản trình duyệt web được Selenium yêu cầu để mở trang web. Tải xuống phiên bản Chrome Webdriver mới nhất từ ​​liên kết bên dưới và lưu nó vào cùng thư mục chứa chương trình chính của bạn.  
    liên kết. https. // chromedriver. crom. tổ chức/tải xuống

Chúng tôi sẽ chia phần mã thành ba phần

  • Thiết lập công cụ webdriver Selenium và Chrome
  • Lấy đầu vào và kiểm tra lỗi đầu vào
  • Dịch bằng Google Dịch

Phần 1. Đặt công cụ Selenium và cài đặt webdriver




from seleniumimport webdriver

from selenium.webdriver.common.keysimport Keys

from selenium.common.exceptionsimport selenium1

from selenium.common.exceptionsimport selenium5

selenium6

selenium7

from selenium9import import1

import2import3 import4

import5_______2_______6import7

import8import3 webdriver0import3_______3_______2webdriver3import3 webdriver5

  • Nhập đối tượng webdriver để kết nối với phiên bản trình duyệt chrome
  • Nhập thư viện phím để kết nối các lệnh bàn phím cơ bản với phiên bản trình duyệt
  • Nhập trình xử lý ngoại lệ cho phiên bản trình duyệt
  • Nhập tùy chọn trình duyệt và đặt thuộc tính '–headless' để chạy phiên bản trình duyệt ở chế độ nền. Nhận xét ” chrome_op. add_argument(‘–headless’)” để đưa webdriver về các quy trình nền trước

Phần 2. Lấy thông tin đầu vào và kiểm tra  đối với thông tin đầu vào




webdriver6 webdriver7

webdriver8webdriver9import3 from1from2from3from4from5from6from3from8from5

selenium.webdriver.common.keys0selenium.webdriver.common.keys1from3_______5_______3from5selenium.webdriver.common.keys5from3selenium.webdriver.common.keys7from5selenium.webdriver.common.keys9from3import1import2

selenium6

webdriver8import5import6import7_______2_______7

webdriver8import5import6_______7_______2_______0_______5Keys4from5Keys6import7

selenium6

webdriver8from0 from1from2 from3

from4_______2_______5from6Keys4_______0_______8

selenium6

webdriver8import5_______2_______6selenium.common.exceptions3selenium.common.exceptions4import3selenium.common.exceptions6

webdriver8selenium.common.exceptions8import3 selenium00_______1_______01

webdriver8selenium03_______2_______3 selenium05

selenium6

webdriver8from0 from1from2 from3

from4_______1_______13selenium14_______2_______3import3 selenium.common.exceptions8selenium18 selenium19 selenium20

selenium21selenium03import3 selenium24

selenium21selenium26

webdriver8selenium13_______2_______6selenium19 selenium20

from4_______2_______5import6selenium35import7

from4_______1_______38

selenium6

webdriver8import5import6selenium43selenium.common.exceptions4import3selenium.common.exceptions6

webdriver8selenium48import3 selenium00_______1_______01

webdriver8selenium53_______2_______3 selenium05

selenium6

webdriver8from0 from1from2 from3

from4_______1_______13selenium14_______2_______3import3 selenium48selenium18 selenium19 selenium70

selenium21selenium53____2_______3 selenium24

selenium21selenium26

selenium6

webdriver8selenium13_______2_______6selenium19 selenium70

from4_______2_______5import6selenium86import7

from4_______1_______38

selenium6

webdriver8selenium13selenium93import3_______2_______3 selenium96

from4_______2_______5import6import00import7

from4_______1_______38

selenium6

webdriver8import5_______2_______6import08selenium.common.exceptions4import3selenium.common.exceptions6

webdriver8import13_______2_______3 selenium00selenium01

selenium6

webdriver8import19 import20

Đây là mã demo nên mã ngôn ngữ được giới hạn ở {tiếng Anh, tiếng Tây Ban Nha, tiếng Đức, tiếng Ý, tiếng Pháp}. Bạn có thể thêm nhiều ngôn ngữ và mã của chúng sau

  • Lấy đầu vào cho ngôn ngữ nguồn và mã ngôn ngữ đích
  • Kiểm tra xem các mã đã nhập có được hỗ trợ hay không
  • Ngôn ngữ nguồn và mã ngôn ngữ đích không được giống nhau.  

Phần 3. Dịch bằng Google Dịch




webdriver6 import22

webdriver8import24_______2_______3 import26

webdriver8import28from3

from4_______2_______31

from4_______2_______33import34import3import3 import37

selenium21import24_______2_______3 import41

selenium6

webdriver8import44 import45

from4_______2_______5import48

selenium6

webdriver8import44 import52

from4_______2_______5import55

selenium6

webdriver8selenium13_______2_______59import3 import37

from4_______2_______19 import24

webdriver8import66from3

from4_______2_______19 import70

selenium6

selenium6

webdriver6 import74

webdriver8import76_______2_______3 import78 import79 import80

from4_______9_______8import79 import84 import79 selenium48import79import88import79import13

webdriver8import92import3 import94

webdriver8import19 import97import98import7

  • hàm googleTranslate() nhận ba tham số i. e. mã nguồn, mã đích và cụm từ. Nó tạo URL để trình duyệt yêu cầu.  
  • tập lệnh chứa câu lệnh javascript tìm kiếm phần tử HTML với class = “tlid-translation” và trả về nội dung văn bản của nó
  • hàm makeCall() đưa ra yêu cầu với URL được tạo, thực thi tập lệnh khi trang web sẵn sàng và trả về văn bản đã tìm nạp

Kết hợp ba phần trên




selenium13 webdriver01_______2_______3import3 webdriver04from3

webdriver8import20import3 webdriver09

webdriver8import5_______2_______6webdriver13webdriver14

Dán tất cả các phần được hiển thị ở trên vào một. py và thực thi nó bằng Python3

Execution: python3

đầu ra.  

phần đầu vào

Google dịch Python sang javascript

Nếu bạn đã nhận xét câu lệnh thuộc tính '–headless', thì một cửa sổ trình duyệt như bên dưới sẽ xuất hiện

Google dịch Python sang javascript

Kết quả sẽ xuất hiện trên cửa sổ terminal như bên dưới

Google dịch Python sang javascript

Ghi chú. Đây là dự án demo nên ngôn ngữ được hỗ trợ bị hạn chế. Bạn có thể tăng hỗ trợ ngôn ngữ bằng cách thêm nhiều mã ngôn ngữ trong phần khai báo

Bạn có thể biên dịch Python thành JavaScript không?

Có thể biên dịch mã Python thành javascript và tải mã sau đó bằng cách sử dụng câu lệnh nhập . Xem brython. vé. 222 để biết thêm chi tiết. Bạn sẽ phải tải brython js lib trước vì cuối cùng, ngữ nghĩa của Python khá khác so với Javascript. Bạn có thể bao gồm biên dịch.

Làm cách nào để sử dụng Google Dịch với JavaScript?

Cách sử dụng API Google Dịch với JavaScript. Tạo biểu mẫu web có tính năng dịch .
Tạo HTML tĩnh. Chúng tôi bắt đầu với trang HTML cơ bản. .
Thêm trình xử lý nhấp chuột thả xuống. .
Thêm lệnh gọi JQuery AJAX để gọi API Google Dịch. .
Thêm mã cập nhật trình giữ chỗ biểu mẫu. .
Thêm Đăng nhập cho Trình xử lý sự kiện “nhấp chuột”

Tôi có thể sử dụng Google Dịch bằng Python không?

Googletrans là thư viện python miễn phí và không giới hạn đã triển khai API Google Dịch. Điều này sử dụng API Google Dịch Ajax để thực hiện cuộc gọi đến các phương thức như phát hiện và dịch. Tương thích với Python 3. 6+ .

Làm cách nào để sử dụng API Google Translator trong Python?

Sử dụng API dịch với Python .
Tổng quan. .
Thiết lập và yêu cầu
Kích hoạt API. .
Xác thực các yêu cầu API. .
Cài đặt thư viện máy khách. .
Bắt đầu Python tương tác. .
Liệt kê các ngôn ngữ có sẵn. .
Dịch văn bản