Search code examples
pythonurllib2

Sending input to a webpage and reaching for output via Python


I am building a program in Python that needs to go through this webpage: http://circe.med.uniroma1.it/proABC/index.php

Instead of doing it manually, I wonder how do you send information to the website ? There are two DNA sequences from files, that can be used directly from the file or stored as two strings in Python.

And in the opposite way, how do you access the results through Python ? (I need the positions where there is a pic) Is urllib2 suitable for this ?

Here are two sequences as example :

light = 'DIQMTQSPASLSASVGETVTITCRASGNIHNYLAWYQQKQGKSPQLLVYYTTTLADGVPSRFSGSGSGTQYSLKINSLQPEDFGSYYCQHFWSTPRTFGGGTKLEIKRADAAPTVSIFPPSSEQLTSGGASVVCFLNNFYPKDINVKWKIDGSERQNGVLNSWTDQDSKDSTYSMSSTLTLTKDEYERHNSYTCEATHKTSTSPIVKSFNRNEC'

heavy = 'QVQLKESGPGLVAPSQSLSITCTVSGFSLTGYGVNWVRQPPGKGLEWLGMIWGDGNTDYNSALKSRLSISKDNSKSQVFLKMNSLHTDDTARYYCARERDYRLDYWGQGTTLTVSSASTTPPSVFPLAPGSAAQTNSMVTLGCLVKGYFPEPVTVTWNSGSLSSGVHTFPAVLQSDLYTLSSSVTVPSSPRPSETVTCNVAHPASSTKVDKKIVPRDC'

Solution

  • You can use robobrowser. Please see the github page: https://github.com/jmcarp/robobrowser

    Example code:

    from robobrowser import RoboBrowser
    browser = RoboBrowser(history=True)
    browser.open('http://circe.med.uniroma1.it/proABC/index.php')
    form = browser.get_forms()[1]
    # Now you can fill each elements in form as given below
    form['light']='DIQMTQSPASLSASVGETVTITCRASGNIHNYLAWYQQKQGKSPQLLVYYTTTLADGVPSRFSGSGSGTQYSLKINSLQPEDFGSYYCQHFWSTPRTFGGGTKLEIKRADAAPTVSIFPPSSEQLTSGGASVVCFLNNFYPKDINVKWKIDGSERQNGVLNSWTDQDSKDSTYSMSSTLTLTKDEYERHNSYTCEATHKTSTSPIVKSFNRNEC'
    form['heavy']='QVQLKESGPGLVAPSQSLSITCTVSGFSLTGYGVNWVRQPPGKGLEWLGMIWGDGNTDYNSALKSRLSISKDNSKSQVFLKMNSLHTDDTARYYCARERDYRLDYWGQGTTLTVSSASTTPPSVFPLAPGSAAQTNSMVTLGCLVKGYFPEPVTVTWNSGSLSSGVHTFPAVLQSDLYTLSSSVTVPSSPRPSETVTCNVAHPASSTKVDKKIVPRDC'
    

    and so on.. You can simply use form['element-name'] to fill out the form.

    Finally submit the form after filling all the mandatory fields:

     browser.submit_form(form)
    

    Please read see the Github Readme for more info on how to access the result.