I want to perform a login to a vbulletin forum.
After the login you will be redirected to the mainpage. I want the content of a forum thread which is only visible, when logged in.
I found this question: Login to Vbulletin forum with python
My code looks like this:
username = "testuser"
password = "testpassword"
try:
FORUM_URL = 'https://forumurl.com/forum/'
session = requests.Session()
session.post(FORUM_URL + 'login.php?do=login', {
'vb_login_username': username,
'vb_login_password': password,
'vb_login_md5password': hashlib.md5(password.encode()).hexdigest(),
'vb_login_md5password_utf': hashlib.md5(password.encode()).hexdigest(),
'cookieuser': 1,
'do': 'login',
's': '',
'securitytoken': 'guest'
})
except Exception as e:
print(str(e))
q = session.get("https://forumurl.com/forum/showthread.php?123456")
print(str(q.text))
This generates the following output: (translated)
<meta http-equiv="refresh" content="1">
<noscript>
<p>Javascript and Cookies need to be enabled</p>
</noscript>
<script>
createCookie("test", "md5hashofcookie");
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
</script>
After this I tried to do the login with firefoxdriver, which also gets the output above.
I also tried to do it with mechanize and used the following code:
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('user-agent', ' Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'),
('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
br.open("https://testforum.com/forum/login.php?do=login")
br.form = list(br.forms())[0]
br["vb_login_username"] = username
br["vb_login_password"] = password
br["vb_login_md5password"] = hashlib.md5(password.encode()).hexdigest()
br["vb_login_md5password_utf"] = hashlib.md5(password.encode()).hexdigest()
br["cookieuser"] = 1
br["do"] = "login"
br["s"] = ""
br["securitytoken"] = "guest"
response = br.submit()
print(response)
This generates the following output:
mechanize._response.httperror_seek_wrapper: HTTP Error refresh: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
OK
I don't know how to do the login with cookies/javascript enabled.
Edit:
I got the solution with Selenium:
driver = webdriver.Firefox()
driver.get("https://12345.com/forum/forum.php")
time.sleep(15)
driver.find_element_by_link_text("Login").click()
time.sleep(5)
driver.find_element_by_class_name("loginbutton").click()
driver.find_element_by_id("vb_login_username").send_keys(username)
driver.find_element_by_id("vb_login_password").send_keys(password)
driver.find_element_by_class_name("button").click()
Sorry, forgot to mark it as answered.
Here is the code again:
driver = webdriver.Firefox()
driver.get("https://12345.com/forum/forum.php")
time.sleep(15)
driver.find_element_by_link_text("Login").click()
time.sleep(5)
driver.find_element_by_class_name("loginbutton").click()
driver.find_element_by_id("vb_login_username").send_keys(username)
driver.find_element_by_id("vb_login_password").send_keys(password)
driver.find_element_by_class_name("button").click()