2012-05-16 40 views
0

我想從網頁獲取所有GET和POST參數。假設有一些網頁。我可以從此頁面獲取所有鏈接。但是如果這個頁面需要輸入參數(GET和POST),我怎麼能得到它們?我的算法是這樣的:如何從python網頁獲取POST和GET參數

find in web page this type of strings <form method="GET">...</form>; 
then for each found result: 
    get <input> fields and construct request 
    then save it somewhere 

我的目的是寫履帶它可以獲取所有鏈接,從網站GET和POST參數,然後保存在某個地方它進行進一步的分析。我的算法很簡單,所以我想知道有沒有其他方法(在python中)?你能推薦任何Python庫嗎?

+0

PLZ,描述你想要做什麼。 – Denis

+0

我想要寫一個Web應用程序漏洞掃描器履帶,所以這種履帶必須得到所有環節,GET從這個網頁POST參數,並將它們存儲到分析漏洞掃描器 – torayeff

+0

簡單地說,你需要頁面上的所有鏈接和形式?如果它是真的,你可以嘗試使用ButifulSoup或lxml,我更喜歡最後一個。 – Denis

回答

0

如何這樣的事情,讓你開始了嗎?它翻出形式和輸入屬性:

from BeautifulSoup import BeautifulSoup 

s = urllib2.urlopen('http://stackoverflow.com/questions/10614974/how-to-get-post-and-get-parameters-from-web-page-in-python').read() 
soup = BeautifulSoup(s) 

forms = soup.findall('form') 
for form in forms: 
    print 'form action: %s (%s)' % (form['action'], form['method']) 
    inputs = form.findAll('input') 
    for input in inputs: 
    print " -> %s" % (input.attrs) 

輸出(此頁):

form action: /search (get) 
    -> [(u'autocomplete', u'off'), (u'name', u'q'), (u'class', u'textbox'), (u'placeholder', u'search'), (u'tabindex', u'1'), (u'type', u'text'), (u'maxlength', u'140'), (u'size', u'28'), (u'value', u'')] 
form action: /questions/10614974/answer/submit (post) 
    -> [(u'id', u'fkey'), (u'name', u'fkey'), (u'type', u'hidden'), (u'value', u'923d3d8b45bbca57cbf0b126b2eb9342')] 
    -> [(u'id', u'author'), (u'name', u'author'), (u'type', u'text')] 
    -> [(u'id', u'display-name'), (u'name', u'display-name'), (u'type', u'text'), (u'size', u'30'), (u'maxlength', u'30'), (u'value', u''), (u'tabindex', u'105')] 
    -> [(u'id', u'm-address'), (u'name', u'm-address'), (u'type', u'text'), (u'size', u'40'), (u'maxlength', u'100'), (u'value', u''), (u'tabindex', u'106')] 
    -> [(u'id', u'home-page'), (u'name', u'home-page'), (u'type', u'text'), (u'size', u'40'), (u'maxlength', u'200'), (u'value', u''), (u'tabindex', u'107')] 
    -> [(u'id', u'submit-button'), (u'type', u'submit'), (u'value', u'Post Your Answer'), (u'tabindex', u'110')] 
+0

如果你還沒有安裝BeautifulSoup已經可以通過使用「pip install BeautifulSoup」 –

+0

對於http://www.ertir.com它給出了這個錯誤:回溯(最近呼叫最後): 文件「akaParser.py」,行86,在 getForms(parsedPage) 文件「akaParser.py」,第75行,在getForms method = form ['method'] 文件「/usr/local/lib/python2.7/dist-packages/bs4/element.py」,第881行,在__getitem__ return self.attrs [鍵] KeyError:'方法' – torayeff

+0

你能解釋什麼是錯誤? – torayeff