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!!
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.
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.
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.
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.
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 print
ing 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…)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With