2010-10-03 49 views

回答

0

我會建議使用這兩種機械化和etree,但我不是程序員,所以不要把我的話。注意:所有代碼都在python中,並且是版本2.7.1,但應該可以達到2.7.3。

希望我能幫助 - 只是另一個傻瓜

import mechanize 
import lxml.etree as etree 

url = 'something' 

br = mechanize.Browser() 
resp = br.open(url) 
parser = etree.parser() 
tree = etree.parse(resp,parser) 
forms = list(br.forms()) 
id_info = {} 
for form in forms: 
    elements = form.controls 
    for element in elements: 
     id_info[element.attrs['id']]='' 

inputs = tree.findall('.//input') 
for i in inputs: 
    index = list(i.getparent()).index(i) 
    id_info[i.attrib['id']] = list(i.getparent)[index+1] 

for j in id_info: 
    print j,id_info(j)