I’ve written this to try and log onto a forum (phpBB3).
import urllib2, re import urllib, re logindata = urllib.urlencode({'username': 'x', 'password': 'y'}) page = urllib.urlopen('http://www.woarl.com/board/ucp.php?mode=login'[logindata]) output = page.read()
However when I run it it comes up with;
Traceback (most recent call last): File 'C:/Users/Mike/Documents/python/test urllib2', line 4, in <module> page = urllib.urlopen('http://www.woarl.com/board/ucp.php?mode=login'[logindata]) TypeError: string indices must be integers
any ideas as to how to solve this?
edit
adding a comma between the string and the data gives this error instead
Traceback (most recent call last): File 'C:/Users/Mike/Documents/python/test urllib2', line 4, in <module> page = urllib.urlopen('http://www.woarl.com/board/ucp.php?mode=login',[logindata]) File 'C:\Python25\lib\urllib.py', line 84, in urlopen return opener.open(url, data) File 'C:\Python25\lib\urllib.py', line 192, in open return getattr(self, name)(url, data) File 'C:\Python25\lib\urllib.py', line 327, in open_http h.send(data) File 'C:\Python25\lib\httplib.py', line 711, in send self.sock.sendall(str) File '<string>', line 1, in sendall TypeError: sendall() argument 1 must be string or read-only buffer, not list
edit2
I’ve changed the code from what it was to;
import urllib2, re import urllib, re logindata = urllib.urlencode({'username': 'x', 'password': 'y'}) page = urllib2.urlopen('http://www.woarl.com/board/ucp.php?mode=login', logindata) output = page.read()
This doesn’t throw any error messages, it just gives 3 blank lines. Is this because I’m trying to read from the log in page which disappears after logging in. If so how do I get it to display the index which is what should appear after hitting log in.
Your line
is semantically invalid Python. Presumably you meant
which has a comma separating the arguments. However, what you ACTUALLY want is simply
without trying to enclose logindata into a list and using the more up-to-date version of urlopen is the urllib2 library.