从Python页面上执行JS代码 [英] execute JS code on page from Python

查看:59
本文介绍了从Python页面上执行JS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是使用Python从任何网页上获取命名函数的列表.

My task is to get a list of named functions from any web-page using Python.

我有一个使用JavaScript编写的脚本.它可以满足我的需求.

I have a script written using JavaScript. It does what I need.

加载页面后,我可以从JS控制台(例如,从GoogleChrome中的dev-tools)运行脚本.我有作为结果的函数名称的数组.好吧,但是我转到页面并从浏览器手动执行脚本.但是问题是要从Python进行同样的操作.它可能看起来像这样:

When page is loaded I can run the script from JS console (e.g. from dev-tools in GoogleChrome). I have the array of names of the functions as the result. Well, but I go to the page and execute the script from browser manually. But the question is to do the same from Python. It can look something like this:

def get_named_functions_list(url):
    myscript = settings.get_js_code()  # here I get script that I told above

    tool.open(url)

    while not tool.document.READY: # here I wait while the page will completely loaded
        pass

    js_result = tool.execute_from_console(myscript)

    return list(js_result.values())

那么,Python中是否有一个工具可以自动解决问题?

So, is there a tool in Python that helps to solve the problem automatically?

更新:更清楚地说,我可以将任务划分为子任务列表(在Python中):

UPDATE: To be more clear I can divide the task to the list of subtasks (in Python):

  1. 请求给定网址
  2. 等待document.ready(function ...)完成.
  3. 执行我的JS代码(就像在浏览器中一样).
  4. 获取JS代码返回的结果.

推荐答案

我已经解决了使用然后,我下载了PhantomJS驱动程序以使用没有浏览器窗口的selenium,并将其添加到PATH.

Then I have downloaded the PhantomJS driver to use selenium without a browser window and added it to PATH.

最后,我使用以下Python脚本:

Finally, I use the following Python script:

from selenium import webdriver
    
myscript = settings.get_js_code() # here I get content of *.js file
driver = webdriver.PhantomJS()
driver.get(url)
result = driver.execute_script(myscript)
driver.quit()

注意:您的脚本必须返回一些内容才能得到结果.

Note: your script have to return something to get the result.

这篇关于从Python页面上执行JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆