通过Firefox和Python中的Mozrepl插件,我做到了:
>>> import telnetlib
>>> tn = telnetlib.Telnet(r'127.0.0.1', 4242, 5)
>>> tn.read_eager()
'\nWelcome to MozRepl.\n\n - If you get stuck at the "'
>>> tn.read_until("repl> ")
...snip...
>>> tn.write(r'alert(window.content.location.href)'+"\n")然后我收到一个警告框,其中包含活动选项卡的URL。但是如何将该URL读取到python变量中呢?像tn.write(r';var zz = window.content.location.href'+ "\n")这样的东西,但这并不能让它进入python。
如果能帮上忙我会很感激的。
发布于 2013-10-15 07:19:57
回答有点晚,但希望有用……
您只需再次从telnet连接读取,mozrepl的输出就会出现。一定要注意返回的换行符和带引号的字符串。
发布于 2015-04-02 05:18:23
这可以使用pymozrepl模块来完成。
>>> import mozrepl
>>> repl = mozrepl.Mozrepl()
>>>
>>> repl.execute('alert(window.content.location.href)')
>>>
>>> #or
>>> from mozrepl.type import Raw
>>> repl.execute('alert')(Raw('window.content.location.href'))
>>>
>>> #or
>>> repl.execute('alert')(repl.execute('window').content.location.href)
>>>
>>> #or
>>> repl.execute('alert')(repl.execute('window.content.location.href'))
>>>
>>> #pymozrepl module also can get javascript value.
>>> repl.execute('repl._name')
'repl'https://stackoverflow.com/questions/17624151
复制相似问题