Executing Javascript from Python - Stack Overflow
web scraping - Download CSS and JS using python - Stack Overflow
Downloading Javascript File from Website using Python - Stack Overflow
Best Python Resource for a JavaScript Developer?
Is the Python to JavaScript converter free?
Can I also convert JavaScript back to Python?
What types of Python code can be converted to JavaScript?
Videos
You can also use Js2Py which is written in pure python and is able to both execute and translate javascript to python. Supports virtually whole JavaScript even labels, getters, setters and other rarely used features.
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
Advantages of Js2Py include portability and extremely easy integration with python (since basically JavaScript is being translated to python).
To install:
pip install js2py
Using PyV8, I can do this. However, I have to replace document.write with return because there's no DOM and therefore no document.
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 "))
Or you could create a mock document object
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
I'm a full stack javascript developer and I want to dive into Python world of programming.
Since I'm not a beginner, I would prefer a project based course. What would you suggest to go into free or paid? I looked at Angela Yu's 100 Days of Python Udemy course and loved the concept since it goes with only projects simple to advanced but many people say it's outdated.
Is CS50P still good if I'm not a programming beginner?
Iโm trying to automate a process for the test team at work in which a Python script downloads some Robot Framework test logs using requests and parses the tables using BeautifulSoup. It seemed simple enough; however, I noticed it only works if I download the file first and then open it in my browser and re-save it. Upon comparing the files, it seems as though there is a bunch of JavaScript in the file that is used to render everything, and simply downloading the file via requests downloads the pre-rendered file, which is predominantly comprised of <script> tags.
So I found requests_html and can download the log files, but the render() function never completes, which I discovered may be to a bug in the asyncio library, which is utilized by the function.
I am going to be implementing this in Jenkins, and our agents do not have any browser support; so I do not believe the traditional Selenium approach will work. The only alternative I can think of is a headless browser, but may be unaware of other options.
I should also mention that all of this occurs behind a corporate proxy, so proxy-friendly solutions are a huge plus. Ideally, something to just render the JavaScript would be the optimal solution, since I can already download the pre-rendered HTML.
tl;dr, I need to use JavaScript to render HTML and save it to a file using Python, but canโt use any GUIs and requests_html doesnโt work.
Edit: I ended up using a headless browser to get the rendered page source after I downloaded it locally.
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
driver = Chrome(options=options)
driver.get('file:///path/to/local/HTML/file')
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')