Submitting to a web form using python

前端 未结 3 1657
灰色年华
灰色年华 2020-12-01 07:44

I have seen questions like this asked many many times but none are helpful

Im trying to submit data to a form on the web ive tried requests, and urllib and none have

3条回答
  •  抹茶落季
    2020-12-01 08:38

    Mechanize library from python is also great allowing you to even submit forms. You can use the following code to create a browser object and create requests.

    import mechanize,re
    br = mechanize.Browser()
    br.set_handle_robots(False)   # ignore robots
    br.set_handle_refresh(False)  # can sometimes hang without this
    br.addheaders = [('User-agent', 'Firefox')]             
    br.open( "http://google.com" )
    br.select_form( 'f' )
    br.form[ 'q' ] = 'foo'
    br.submit()
    resp = None
    
    for link in br.links():
        siteMatch = re.compile( 'www.foofighters.com' ).search( link.url )
    
        if siteMatch:
            resp = br.follow_link( link )
            break
    
    content = resp.get_data()
    print content
    

提交回复
热议问题