2011-09-14 15 views
1

我正嘗試使用以下代碼提交表單http://www.harshtechtalk.com/contact-us-harsh-tech-talk,但沒有成功。請幫忙!無法在python中使用urllib2提交網頁表單

#!C:/Python27/python.exe 

import urllib 
import urllib2 

def main(): 
     proxy_info={ 
         'user' : '[email protected]', 
         'pass' : 'xyz', 
         'host' : 'xxxxxxxx', 
         'port' : 80 
         } 
     proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)[email protected]%(host)s:%(port)d" 
% proxy_info}) 
     opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler) 
     urllib2.install_opener(opener) 
     url = 'http://www.harshtechtalk.com/contact-us-harsh-tech-talk' 
     values = { 
         'your-name':'test_name', 
         'your-email':'[email protected]', 
         'your-subject':'python_test', 
         'your-message':'test_message' 
         } 
     data = urllib.urlencode(values) 
     req = urllib2.Request(url,data) 
     response = urllib2.urlopen(req) 
     the_page = response.read() 
     print "done" 

if __name__ == '__main__': 
     main() 

PS - 我不想使用「機械化」,因爲它不支持JavaScript。請讓我知道是否有任何其他模塊可用於處理動態Web表單。

+0

「不成功」是什麼意思?你現在怎麼執行JavaScript? – geoffspear

+0

這是我博客上的聯繫表單。所以,對於任何表單提交,我收到一封電子郵件。沒有成功,我的意思是我沒有收到任何郵件。它使用機械化,因爲我曾嘗試使用它。所以,表單沒有問題。 – theharshest

回答

0

處理AJAX網頁表單通常需要使用selenium,以便Python使用安裝的JavaScript引擎驅動真正的Web瀏覽器。有些人也談論phantomjs,但我現在還不知道它的官方Python模塊。另見:

Headless, scriptable Firefox/Webkit on linux?

+1

硒爲我工作。但對於Pyhton來說,很難找到好的文檔。 – theharshest