如何将Mozrepl Javascript结果读入Python变量?

2024-10-01 07:31:10 发布

您现在位置:Python中文网/ 问答频道 /正文

在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。在

我会很感激你的帮助。在


Tags: import插件urlreadlocationcontentfirefoxwindow
2条回答

这可以使用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'

回答有点晚,但希望有用。。。在

您只需再次读取telnet连接,mozrepl的输出就会出现在那里。一定要注意返回的新行和引用的字符串。在

相关问题 更多 >