2013-07-12 2 views
0

내가 할 :Mozrepl Javascript 결과를 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을 파이썬 변수로 어떻게 읽습니까? tn.write(r';var zz = window.content.location.href'+ "\n")과 비슷한데 파이썬에는 들어 가지 않습니다.

도움에 감사드립니다.

답변

0

비트는 ... 대답 늦은 그러나 희망 유용

당신은 다시 텔넷 연결에서 읽고 mozrepl의 출력이있을 것입니다. 반환 된 줄 바꿈 및 인용 문자열에주의하십시오.

0

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' 
관련 문제