Python 3.2 lxml fill and submit form, select multiple, how to do it? value not working -


great page one, coming perl world , after several years of doing nothing, i've re-started program again (this web page didn't exist, how things change). , now, after 2 full-days of searching, play last card of asking here help.

working under mac environment, python 3.2 , lxml 2.3 (installed following www.jtmoon.com/?p=21), trying do:

my code. put several attempts , output code.

from lxml.html import parse, submit_form, tostring page = parse('http://biodbnet.abcc.ncifcrf.gov/db/db2db.php').getroot() page.forms[0].fields['input'] = 'gi number' page.forms[0].inputs['outputs[]'].value = 'gene id' page.forms[0].fields['hascomma'] = 'no' page.forms[0].fields['removedupvalues'] = 'yes' page.forms[0].fields['request'] = 'db2db' page.forms[0].action = 'http://biodbnet.abcc.ncifcrf.gov/db/db2dbres.php' page.forms[0].fields['idlist'] = '86439006' submit_form(page.forms[0]) 

output:

file "/users/gerard/desktop/barbacue/mgftoxml.py", line 30, in <module> page.forms[0].inputs['outputs[]'].value = 'gene id' file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/site-packages/lxml/html/__init__.py", line 1058, in _value__set "you must pass in sequence") typeerror: must pass in sequence 

so, since element multi-select element, understand have give list

page.forms[0].inputs['outputs[]'].value = list('gene id') 

output:

file "/users/gerard/desktop/barbacue/mgftoxml.py", line 30, in <module> page.forms[0].inputs['outputs[]'].value = list('gene id') file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/site-packages/lxml/html/__init__.py", line 1059, in _value__set self.value.clear() file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/site-packages/lxml/html/_setmixin.py", line 115, in clear self.remove(item) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/site-packages/lxml/html/__init__.py", line 1159, in remove "the option %r not selected" % item) valueerror: option 'affy id' not selected 

'affy id' first option value of list, , not selected. what's problem it?

surprisingly, if instead put

page.forms[0].inputs['outputs[]'].multiple = list('gene id') #page.forms[0].inputs['outputs[]'].value = list('gene id') 

then, somehow lxml likes it, , move on. however, multiple attribute should boolean (actually if print value), shouldn't touch it, , "value" of item should point selected items, according lxml docs.

the new output

file "/users/gerard/desktop/barbacue/mgftoxml.py", line 87, in <module> submit_form(page.forms[0]) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/site-packages/lxml/html/__init__.py", line 856, in submit_form return open_http(form.method, url, values) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/site-packages/lxml/html/__init__.py", line 876, in open_http_urllib return urlopen(url, data) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/urllib/request.py", line 138, in urlopen return opener.open(url, data, timeout) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/urllib/request.py", line 364, in open req = meth(req) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/urllib/request.py", line 1052, in do_request_ raise typeerror("post data should bytes" typeerror: post data should bytes or iterable of bytes. cannot str. 

so, can done?? sure python 2.6 use mecanize, or perhaps lxml work? don't want code in sort-of deprecated version. enjoying lot python, starting consider going perl. perhaps smart movement??

any hugely appreciated

gerard

  • reading in forum, find pythonpaste.org, replacement lxml?

passing in sequence list() generate list sequence. 'gene id' sequence (namely sequence of characters). list('gene id') generate list of characters, so:

>>> list('gene id') ['g', 'e', 'n', 'e', ' ', 'i', 'd'] 

that's not want. try this:

>>> ['gene id'] ['gene id'] 

in other words:

page.forms[0].inputs['outputs[]'].value = ['gene id'] 

that should take bit forward.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -