Trong bài viết này, chúng tôi sẽ thảo luận về cách chạy tệp JavaScript với Python. Đối với điều này, chúng tôi sẽ sử dụng thời gian chạy JS2Py [JavaScript trong mô -đun Python Pure Python]. JS2Py hoạt động bằng cách dịch JavaScript trực tiếp thành Python. Nó chỉ ra rằng bạn có thể chạy JS trực tiếp từ mã Python mà không cần cài đặt các động cơ bên ngoài lớn như V8.JS2PY[Javascript Runtime in Pure Python] Python module. JS2PY works by translating JavaScript directly into Python. It indicates that you may run JS directly from Python code without installing large external engines like V8.
Để sử dụng mô-đun, trước tiên nó phải được cài đặt vào hệ thống, vì nó không được tích hợp.
Syntax:
pip install js2py
Để sử dụng mô -đun, nó phải được nhập.
Syntax:
import js2py
Bây giờ để vượt qua JavaScript thành Python, lệnh JavaScript được lưu trữ dưới dạng một chuỗi trong một số biến. Bây giờ chúng tôi sẽ sử dụng hàm eval_js [] của mô -đun JS2Py và chuyển mã JavaScript cho nó.eval_js[] function of the module js2py, and pass the javascript code to it.
Hàm eval_js [] được xác định trong mô -đun JS2Py, được sử dụng để đánh giá mã JavaScript, truyền mã JavaScript dưới dạng tham số trong mô -đun eval_js. function is defined under the js2py module, which is used to evaluate javascript code, Pass the Javascript code as a parameter in the eval_js module.
Syntax:
JS2Py.EVAL_JS [Mã JavaScript]
Ví dụ: Chạy lệnh JS đơn giản trong Python & NBSP;Running a simple JS command in Python
Python3
import js2py1
import js2py2
import js2py3
import js2py4
import js2py5
import js2py6
import js2py4
import js2py8
import js2py9
0
1
2Output:
10
Bây giờ chúng ta hãy xem xét cách một tệp JS được giải thích trong Python. Đối với tệp *.js đầu tiên này được dịch thành tệp *.py *.js file is translated to *.py file
Mô -đun JS2Py cung cấp một cách để chuyển đổi mã JS thành mã Python, chúng ta phải sử dụng hàm dịch_file [] cho điều này. Sau khi dịch, chúng tôi sẽ nhập tệp Python và cung cấp một cái gì đó cho chức năng được khai báo trong tệp JavaScript.JS code to Python code we must use the translate_file[] function for this. After the translation, we will import the Python file and provide something to the function declared within the javascript file.
Hàm dịch_file [] chấp nhận hai đối số: tệp javascript và tệp python, cuối cùng nó chuyển đổi tệp javascript thành tệp python. function accepts two arguments: a Javascript file and a Python file, finally it converts the Javascript file to a Python file.
Syntax:
js2py.translate_file [tệp javascript, tệp python]
Ví dụ: & nbsp; chạy tệp js bằng pythonRunning a JS file using Python
Tệp JavaScript:
JavaScript
3
4
5
6
7
8
9import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
0import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
1Tệp Python: & NBSP;
Python3
import js2py1
import js2py2
import js2py3
import js2py4
import js2py5
import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
8import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
9pip install js2py
0pip install js2py
1import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
0pip install js2py
3pip install js2py
4import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
0Output:
import js2py6import js2py4import js2py8
Bây giờ chúng ta hãy xem xét cách một tệp JS được giải thích trong Python. Đối với tệp *.js đầu tiên này được dịch thành tệp *.py
Mô -đun JS2Py cung cấp một cách để chuyển đổi mã JS thành mã Python, chúng ta phải sử dụng hàm dịch_file [] cho điều này. Sau khi dịch, chúng tôi sẽ nhập tệp Python và cung cấp một cái gì đó cho chức năng được khai báo trong tệp JavaScript.It is defined under the js2py module, which is used to run the Javascript file. It takes a Javascript file as an argument.
Syntax:
Hàm dịch_file [] chấp nhận hai đối số: tệp javascript và tệp python, cuối cùng nó chuyển đổi tệp javascript thành tệp python.
js2py.translate_file [tệp javascript, tệp python]Running JS in Python
Python3
import js2py1
import js2py2
import js2py3
import js2py4
import js2py5
import PyV8
ctx = PyV8.JSContext[]
ctx.enter[]
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
"""
print ctx.eval[js.replace["document.write", "return "]]
3pip install js2py
4import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
0Output:
import js2py6import js2py4import js2py8
Tôi có các trang web HTML mà tôi đang bò bằng cách sử dụng XPath.
import PyV8
ctx = PyV8.JSContext[]
ctx.enter[]
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
"""
print ctx.eval[js.replace["document.write", "return "]]
6 của một nút nhất định cung cấp cho tôi chuỗi này:
Tôi chỉ cần đầu ra của
import PyV8
ctx = PyV8.JSContext[]
ctx.enter[]
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
"""
print ctx.eval[js.replace["document.write", "return "]]
7. Tôi có thể viết một regex để tìm ra toàn bộ, nhưng tôi muốn mã của mình giữ gọn gàng. Sự thay thế tốt nhất là gì?Tôi đang vượt qua các thư viện sau, nhưng tôi không thấy một giải pháp chính xác. Hầu hết trong số họ đang cố gắng mô phỏng trình duyệt, làm cho mọi thứ sên chậm.
- //code.google.com/p/python-pidermonkey/ [nói rõ ràng
8]import PyV8 ctx = PyV8.JSContext[] ctx.enter[] js = """ function escramble_758[]{ var a,b,c a='+1 ' b='84-' a+='425-' b+='7450' c='9' document.write[a+c+b] } escramble_758[] """ print ctx.eval[js.replace["document.write", "return "]]
- //code.google.com/p/webscraping/ [không thấy gì cho javascript, tôi có thể sai]
- //pypi.python.org/pypi/selenium [Trình duyệt mô phỏng]
Chỉnh sửa: Một ví dụ sẽ rất tuyệt .. [Barebones sẽ làm]
DDA
5,8292 Huy hiệu vàng24 Huy hiệu bạc34 Huy hiệu đồng2 gold badges24 silver badges34 bronze badges
Hỏi ngày 13 tháng 4 năm 2012 lúc 6:39Apr 13, 2012 at 6:39
6
Bạn cũng có thể sử dụng JS2Py được viết bằng Pure Python và có thể cả thực thi và dịch JavaScript sang Python. Hỗ trợ hầu như toàn bộ JavaScript ngay cả các nhãn, getters, setters và các tính năng hiếm khi được sử dụng khác.
import js2py
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
""".replace["document.write", "return "]
result = js2py.eval_js[js] # executing JavaScript and converting the result to python string
Ưu điểm của JS2Py bao gồm tính di động và tích hợp cực kỳ dễ dàng với Python [vì về cơ bản JavaScript đang được dịch sang Python].
Để cài đặt:
pip install js2py
Đã trả lời ngày 29 tháng 5 năm 2015 lúc 19:08May 29, 2015 at 19:08
Piotr dabkowskipiotr dabkowskiPiotr Dabkowski
5.4534 Huy hiệu vàng37 Huy hiệu bạc47 Huy hiệu đồng4 gold badges37 silver badges47 bronze badges
1
Sử dụng pyv8, tôi có thể làm điều này. Tuy nhiên, tôi phải thay thế
import PyV8
ctx = PyV8.JSContext[]
ctx.enter[]
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
"""
print ctx.eval[js.replace["document.write", "return "]]
9 bằng class MockDocument[object]:
def __init__[self]:
self.value = ''
def write[self, *args]:
self.value += ''.join[str[i] for i in args]
class Global[PyV8.JSClass]:
def __init__[self]:
self.document = MockDocument[]
scope = Global[]
ctx = PyV8.JSContext[scope]
ctx.enter[]
ctx.eval[js]
print scope.document.value
0 vì không có dom và do đó không có class MockDocument[object]:
def __init__[self]:
self.value = ''
def write[self, *args]:
self.value += ''.join[str[i] for i in args]
class Global[PyV8.JSClass]:
def __init__[self]:
self.document = MockDocument[]
scope = Global[]
ctx = PyV8.JSContext[scope]
ctx.enter[]
ctx.eval[js]
print scope.document.value
1.import PyV8
ctx = PyV8.JSContext[]
ctx.enter[]
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
"""
print ctx.eval[js.replace["document.write", "return "]]
Hoặc bạn có thể tạo một đối tượng tài liệu giả
class MockDocument[object]:
def __init__[self]:
self.value = ''
def write[self, *args]:
self.value += ''.join[str[i] for i in args]
class Global[PyV8.JSClass]:
def __init__[self]:
self.document = MockDocument[]
scope = Global[]
ctx = PyV8.JSContext[scope]
ctx.enter[]
ctx.eval[js]
print scope.document.value
Daniel f
13.3k11 Huy hiệu vàng84 Huy hiệu bạc113 Huy hiệu đồng11 gold badges84 silver badges113 bronze badges
Đã trả lời ngày 13 tháng 4 năm 2012 lúc 7:07Apr 13, 2012 at 7:07
Kien Truongkien TruongKien Truong
Huy hiệu vàng 11k2 30 Huy hiệu bạc34 Huy hiệu đồng2 gold badges30 silver badges34 bronze badges
6
Thêm một giải pháp nữa khi PYV8 dường như không được công nhận và phụ thuộc vào phiên bản cũ của LIBV8.
Pyminiracer Đây là một gói xung quanh động cơ V8 và nó hoạt động với phiên bản mới và được duy trì tích cực.
class MockDocument[object]:
def __init__[self]:
self.value = ''
def write[self, *args]:
self.value += ''.join[str[i] for i in args]
class Global[PyV8.JSClass]:
def __init__[self]:
self.document = MockDocument[]
scope = Global[]
ctx = PyV8.JSContext[scope]
ctx.enter[]
ctx.eval[js]
print scope.document.value
2from py_mini_racer import py_mini_racer
ctx = py_mini_racer.MiniRacer[]
ctx.eval["""
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
return a+c+b;
}
"""]
ctx.call["escramble_758"]
Và vâng, bạn phải thay thế
import PyV8
ctx = PyV8.JSContext[]
ctx.enter[]
js = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write[a+c+b]
}
escramble_758[]
"""
print ctx.eval[js.replace["document.write", "return "]]
9 bằng class MockDocument[object]:
def __init__[self]:
self.value = ''
def write[self, *args]:
self.value += ''.join[str[i] for i in args]
class Global[PyV8.JSClass]:
def __init__[self]:
self.document = MockDocument[]
scope = Global[]
ctx = PyV8.JSContext[scope]
ctx.enter[]
ctx.eval[js]
print scope.document.value
0 như những người khác đề xuấtĐã trả lời ngày 21 tháng 3 năm 2018 lúc 9:16Mar 21, 2018 at 9:16
DienowdienowDienow
1.19710 Huy hiệu bạc13 Huy hiệu đồng10 silver badges13 bronze badges
2
Bạn có thể sử dụng bối cảnh JS2Py để thực thi mã JS của mình và nhận đầu ra từ Document.Write với đối tượng tài liệu giả:
import js2py
js = """
var output;
document = {
write: function[value]{
output = value;
}
}
""" + your_script
context = js2py.EvalJs[]
context.execute[js]
print[context.output]
Đã trả lời ngày 16 tháng 9 năm 2018 lúc 23:46Sep 16, 2018 at 23:46
MirkomirkoMirko
1.9522 huy hiệu vàng18 Huy hiệu bạc16 Huy hiệu đồng2 gold badges18 silver badges16 bronze badges
Bạn có thể sử dụng Yêu cầu-HTML sẽ tải xuống và sử dụng Chromium bên dưới.
from requests_html import HTML
html = HTML[html=""]
script = """
function escramble_758[]{
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
return a+c+b;
}
"""
val = html.render[script=script, reload=False]
print[val]
# +1 425-984-7450
Thêm về điều này đọc ở đây
Đã trả lời ngày 8 tháng 4 năm 2020 lúc 15:19Apr 8, 2020 at 15:19
LevonlevonLevon
9.3704 Huy hiệu vàng44 Huy hiệu bạc41 Huy hiệu đồng4 gold badges44 silver badges41 bronze badges
QuickJS nên là lựa chọn tốt nhất sau khi QuickJS ra mắt. Chỉ cần
class MockDocument[object]:
def __init__[self]:
self.value = ''
def write[self, *args]:
self.value += ''.join[str[i] for i in args]
class Global[PyV8.JSClass]:
def __init__[self]:
self.document = MockDocument[]
scope = Global[]
ctx = PyV8.JSContext[scope]
ctx.enter[]
ctx.eval[js]
print scope.document.value
5 và bạn đã sẵn sàng để đi.Sửa đổi dựa trên ví dụ về readme.
import js2py0
//github.com/PetterS/quickjs
Đã trả lời ngày 31 tháng 10 năm 2019 lúc 6:13Oct 31, 2019 at 6:13
Echoechoecho
2.3481 Huy hiệu vàng21 Huy hiệu bạc16 Huy hiệu đồng1 gold badge21 silver badges16 bronze badges
Thực sự muộn cho bữa tiệc nhưng bạn có thể sử dụng một người kế thừa PYV8 thường được duy trì bởi một tổ chức có uy tín [chủ quan] có tên là CloudFlare. Đây là URL kho lưu trữ:
//github.com/cloudflare/stpyv8
Đã trả lời ngày 13 tháng 7 lúc 16:01Jul 13 at 16:01
Cstayyabcstayyabcstayyab
2782 Huy hiệu bạc19 Huy hiệu đồng2 silver badges19 bronze badges