0

我目前使用ghost.py,当我调用它时他们有一个函数show() 显示网站但立即关闭它。如何保持打开状态?

from ghost import Ghost
import PySide

ghost = Ghost()

with ghost.start() as session:
    page, resources = session.open("https://www.instagram.com/accounts/login/?force_classic_login")
    session.set_field_value("input[name=username]", "joe")
    session.set_field_value("input[name=password]", "test")
    session.show()
    session.evaluate("alert('test')")
4

1 回答 1

0

会话预览将保持打开直到session退出 - 通过session.exit()隐式调用会话上下文。要保持此打开状态,您需要要么不退出会话上下文,要么不使用会话上下文。

前者可以这样实现:

from ghost import Ghost
import PySide

ghost = Ghost()

with ghost.start() as session:
    page, resources = session.open("https://www.instagram.com/accounts/login/?force_classic_login")
    session.set_field_value("input[name=username]", "joe")
    session.set_field_value("input[name=password]", "test")
    session.show()
    session.evaluate("alert('test')")

    # other python code

后者可以这样实现:

from ghost import Ghost
import PySide

ghost = Ghost()

session = ghost.start()
page, resources = session.open("https://www.instagram.com/accounts/login/?force_classic_login")
session.set_field_value("input[name=username]", "joe")
session.set_field_value("input[name=password]", "test")
session.show()
session.evaluate("alert('test')")

# other python code

然而,当 python 进程结束时,会话将不可避免地退出。另外值得注意的是,一些操作会在初始 http 请求完成后立即返回。如果您希望等到其他资源加载完毕,您可能需要调用session.wait_for_page_loaded(). 我还发现某些表单提交需要调用才能session.sleep()按预期运行。

于 2016-05-23T12:03:07.450 回答