【问题标题】:Use python variables inside Ghost.py script在 Ghost.py 脚本中使用 python 变量
【发布时间】:2018-06-05 04:36:02
【问题描述】:

我尝试用 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 方法?我一直在尝试一切,但到目前为止没有运气。

运行 python 2.7、Ghost 0.2.3 和 PyQt4。提前致谢。

【问题讨论】:

    标签: python web-scraping ghost.py


    【解决方案1】:

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

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

    【讨论】:

    • 成功了,我完全忘记了 % 这件事,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2021-06-03
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多