开发者

Problems with Forms, using POST method with Mechanize in Python

开发者 https://www.devze.com 2023-03-12 05:38 出处:网络
开发者_JS百科I was trying to fill a form using mechanize in python, it didn\'t work with normal submit(). Somehow mechanize didn\'t manage to parse the radio buttons and found only 1 instead of 4. Aft

开发者_JS百科I was trying to fill a form using mechanize in python, it didn't work with normal submit(). Somehow mechanize didn't manage to parse the radio buttons and found only 1 instead of 4. After that I tried to write a POST request -

    data = {
        'localid' : '11755',
        'language' : '3',
        'code' : 'hello world',
    }
    page = browser.open( self.submiturl, urllib.urlencode( data) )

But it doesn't post anyhing at all. I am not sure what I am missing here, is it the right way make a POST? Is there any other way to make mechanize recognize radio buttons?

My full code can be read from this link.


sounds like mechanize is having trouble parsing the form, try something along these lines

br = mechanize.Browser()
resp = br.open('your_url_here')
print resp.get_data() # if you want to see what's returned
# if you want to see the forms, so you can find the index of the
# form you want and check that is has all the fields, if it doesn't
# you should should parse the response with BeautifulSoup
for form in br.forms():
    print '---------------'
    print form
br.select_form(nr=0) # to select the first form
br['field_name'] = 'field_value'
br['select_field_name'] = ['select_field_value']
br.submit()
0

精彩评论

暂无评论...
验证码 换一张
取 消