首页 > 解决方案 > 在 Ghost.py 脚本中使用 python 变量

问题描述

我试图用 Ghost.py 多次填写表格,我需要输入到表格中的值在一个列表中,程序如下所示:

inputData = ["input1", "input2", "andSoOn"]

ghost = Ghost()
with ghost.start():
    session = Session(ghost, download_images=False, display=True, user_agent=USER_AGENT)
    page,rs = session.open("http://somesite.org/fillme", timeout=120)
    assert page.http_status == 200

    # this is where i need the input data, to fill the values.
    session.evaluate("""
    document.querySelector('input[name="DataForm"]').value = "dataInput";
    """)

    # Do something and close
    session.webview.setHtml("")
    session.exit()

如何将 inputData 列表中的值传递给脚本内的 querySelector 方法?我一直在尝试一切,到目前为止还没有运气。

使用带有 PyQt4 的 Ghost 0.2.3 运行 python 2.7。提前致谢。

标签: pythonweb-scrapingghost.py

解决方案


您是否尝试过使用 for 循环?

for input in inputData:
    session.evaluate('document.querySelector(\'input[name="DataForm"]\').value= "%s";' % input)

推荐阅读