The html code of the form, and my code are below. I can't get the value to post/submit. Can anyone help?

HTML Code of Form:

<form method='post' autocomplete='off'>
    <input type='hidden' name='action' value='grant-revoke' />
    <input type='hidden' name='creator_badge_index' value='1' />

    <input type='hidden' name='token' value='92dcd92a8bc16f73f330d118ae1ed891' />
    <input type='hidden' name='do-grant' value='1' />
    <div id='grant-div'><span class='label'>Grant badge: </span><input type='text' id='grant-userid' name='grant-userid' value='userid / avatar name' /><input type='submit' value='Grant!' /></div>
</form>

My Code:

opener = urllib.request.build_opener()
cj = http.cookiejar.MozillaCookieJar()
cj.load('C:/Users/xx/Documents/moz_cookies.txt')
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

params = urllib.parse.urlencode({'grant-userid' : 'Guest_xLolKittyx'})
form = opener.open('http://www.imvu.com/catalog/web_manage_badges.php?action=grant-revoke&creator_badge_index=1', params)
data = form.read()
form.close()
print(data)

Recommended Answers

All 5 Replies

Okay, I changed my code a little, it now uses an OpenerDirector..

opener = urllib.request.build_opener()
cj = http.cookiejar.MozillaCookieJar()
cj.load('C:/Users/xx/Documents/moz_cookies.txt')
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

params = urllib.parse.urlencode({'grant-userid' : 'Guest_xLolKittyx'})
form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/web_manage_badges.php?action=grant-revoke&creator_badge_index=1', params)
data = form.read()
form.close()
print(data)

However, now I get this error when the code is run:

Traceback (most recent call last):
  File "C:\Python31\htmlparser.py", line 34, in <module>
    form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/web_manage_badges.php?action=grant-revoke&creator_badge_index=1', params)
  File "C:\Python31\lib\urllib\request.py", line 332, in open
    req = Request(fullurl, data)
  File "C:\Python31\lib\urllib\request.py", line 174, in __init__
    self._parse()
  File "C:\Python31\lib\urllib\request.py", line 179, in _parse
    raise ValueError("unknown url type: %s" % self.full_url)
ValueError: unknown url type: grant-userid=Guest_xLolKittyx

You are mixing GET-type (indicated by ? in the URL) and POST-type
parameters. Put the action and creator_badge_index parameters also in
the dictionary. And probably you need to provide the other hidden fields
from the form also.

Something like (untested):

paramdict = {
          'action': 'grant-revoke',
          'creator_badge_index': '1',
          'token': '92dcd92a8bc16f73f330d118ae1ed891',
          'do-grant': '1',
          'grant-userid' : 'Guest_xLolKittyx',
          }
params = urllib.parse.urlencode(paramdict)
url = 'http://www.imvu.com/catalog/web_manage_badges.php'
form = urllib.request.OpenerDirector.open(url, params)

Thanks for the response! I changed the code, but I still get an error when it is run.. :/

Error Message:

Traceback (most recent call last):
  File "C:\Python31\htmlparser.py", line 40, in <module>
    form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/web_manage_badges.php', params)
  File "C:\Python31\lib\urllib\request.py", line 332, in open
    req = Request(fullurl, data)
  File "C:\Python31\lib\urllib\request.py", line 174, in __init__
    self._parse()
  File "C:\Python31\lib\urllib\request.py", line 179, in _parse
    raise ValueError("unknown url type: %s" % self.full_url)
ValueError: unknown url type: action=grant-revoke&creator_badge_index=1&token=92dcd92a8bc16f73f330d118ae1ed891&do-grant=1&grant-userid=Guest_xLolKittyx

Oh, and I tried it without the hidden fields as well, but I still get an error.

OK, there is another problem that I overlooked.

form = urllib.request.OpenerDirector.open(url, params)

is the wrong way to send the request. OpenerDirector.open is an instance method of the class OpenerDirector, not a class method. So that only works if you make an instance and then call the method on that. Like:

opener = urllib.request.build_opener(....)
form = opener.open(url, params)

That is only useful if you want a special opener, for example with authentication. In the simple cases you can get away with the standard opener by using urlopen:

form = urllib.request.urlopen(url, params)

(this one tested)

commented: Thanks for helping me! +1

Thanks again for the response! Ah, again, it seems I'm still having some trouble.

I'm using an opener, so that my cookies are used when the url request is made. When I use urlopen, I'm not logged in, so nothing works. I tried installing the opener so that I could use urlopen with my cookies, but that didn't help for some reason (still wasn't logged in).

I think I'm still confused on something..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.