2013-05-07 97 views
1

我需要在我的python腳本中解析HTML。然而,要在我的瀏覽器中訪問我需要的頁面,請單擊單選按鈕並提交表單。那麼,我如何使用python提交表單並獲取呈現的html?謝謝,我會很感激任何幫助。用python提交表格

我談論的形式:

<form id="ac" method="post"> 
     <input type="radio" id="a" name="a" value="2" onchange="document.getElementById('ac').submit();" checked="checked"> 
     <input type="radio" id="aa" name="a" value="1" onchange="document.getElementById('ac').submit();"> 
     <input type="radio" id="aaa" name="a" value="0" onchange="document.getElementById('ac').submit();"> 
</form> 

回答

2

可以使用的urllib此:http://docs.python.org/2.7/library/urllib.html

單選按鈕將其數據作爲名稱=值,在這種情況下,A = 2。從Python URLLib/URLLib2 POST

變形例:

import urllib 
import httplib 

server='myserver.com' 
get_data='/link/with/get/data.php?test=1' 

data = urllib.urlencode({'a': 2}) 
h = httplib.HTTPConnection('enfenion.com') 
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} 
h.request('POST', get_data, data, headers) 
r = h.getresponse() 
print r.read() 
+0

感謝。有效。 – Yevs 2013-05-07 19:01:14