Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multiple values for one name urllib2

im trying so submit a webpage that has checkboxes and i need up to 10 of these checkboxes checked

the problem is when i try to assign them to one name in a dict it only assigns the last one not all 10

so how can i do this here is the request code:

forms = {"_ref_ck": ref,
         "type": "create",
         "selected_items[]": sel_itms[0],
         "selected_items[]": sel_itms[1],
         "selected_items[]": sel_itms[2],
         "selected_items[]": sel_itms[3],
         "selected_items[]": sel_itms[4],
         "selected_items[]": sel_itms[5],
         "selected_items[]": sel_itms[6],
         "selected_items[]": sel_itms[7],
         "selected_items[]": sel_itms[8],
         "selected_items[]": sel_itms[9]

         }
data = urllib.urlencode(forms)
req = urllib2.Request('http://www.neopets.com/island/process_tradingpost.phtml',data)
res = self.opener.open(req)
html =  res.read()

this works but i only sends one value for "selected_itmes[]" when i look at the actual request in a web debugging proxy it sends multiple values for "selected_items[]" but i dont know how to do it with python

please help Thank you!!

like image 982
Serial Avatar asked Aug 13 '13 06:08

Serial


People also ask

What is the difference between Urllib and urllib2?

1) urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL. 2) urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

What does Urllib request Urlopen do?

request is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols.

What does urllib2 do in Python?

Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.

What does Urlopen return?

The data returned by urlopen() or urlretrieve() is the raw data returned by the server. This may be binary data (such as an image), plain text or (for example) HTML. The HTTP protocol provides type information in the reply header, which can be inspected by looking at the Content-Type header.


1 Answers

The problem has nothing to do with urlencode; it's that a Python dict can't hold multiple values for the same key. You can see this by printing out forms before you send it—there's only one value there for selected_items[]. That one value gets encoded just fine.

As the documentation explains, there are two ways around this.

First, you can attach a sequence of values to one key, and use the doseq=True flag:

forms = {"_ref_ck": ref,
     "type": "create",
     "selected_items[]": sel_itms[:10]
     }
 data = urllib.urlencode(forms, doseq=True)

Alternatively, you can pass a sequence of two-element tuples instead of a mapping:

forms = (("_ref_ck", ref),
         ("type", "create"),
         ("selected_items[]", sel_itms[0]),
         ("selected_items[]", sel_itms[1]),
         # ...
         )
data = urllib.urlencode(forms)

(You can also use a custom Mapping type that allows repeated keys, instead of a standard dict, but that's probably overkill. Especially since the usual way to construct such a custom Mapping type is by passing it a sequence of key-value pairs…)

like image 150
abarnert Avatar answered Sep 26 '22 23:09

abarnert