[英]How to read Mozrepl Javascript result into a Python Variable?
使用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。
我將不勝感激。
回答的時間有點晚,但希望會有用...
您只需再次從telnet連接中讀取,mozrepl的輸出將在那里。 確保注意返回的換行符和帶引號的字符串。
可以使用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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.