What about using Selenium
instead of urllib
? When doing complex stuff like authentication etc. I prefer to use Selenium
as it is like web browsing. This solution requires an installation of a Firefox.
Installation
>>> sudo pip install selenium
Code
from selenium import webdriver
# initialize the browser and go to the site.
browser = webdriver.Firefox()
url = 'https://www.minecraft.net/login'
user_name = 'your_user_name'
password = 'your_password'
browser.get(url)
# retrieve all necessary page elements
user_name_field = browser.find_element_by_id('username')
password_field = browser.find_element_by_id('password')
user_name_field.send_keys(user_name)
password_field.send_keys(password)
sign_in_btn = browser.find_element_by_id('signin')
# log in
sign_in_btn.click()
I don't own a Minecraft account so I cannot test it, but you probably can retrieve all
Minecraft servers thanks to methods like:
browser.find_elements_by_partial_link_text
browser.find_elements_by_class_name
browser.find_elements_by_css_selector
Generally, I recommend to test your Selenium
code with ipython
, where you can see
the browser work. (Installation: sudo pip install ipython
)
In the case of a lot of Javascript processing - please add the following lines to your code:
browser = webdriver.Firefox()
browser.browser.implicitly_wait(30) # seconds
If you plan to additionally support another browser you can support it with try catch clauses
import sys
browser = None
try:
browser = webdriver.Firefox()
except Exception as e:
try:
browser = webdriver.Chrome()
except Exception as e2:
try:
browser = webdriver.Safari()
except Exception as e3:
print 'Could not find Firefox, Chrome or Safari!'
sys.exit(0)
This nested try-except
clause is not the best programming style, but I am sure it helps
you to use it for your purposes. Taking the tab completion in IPython
Selenium
supports
- Safari
- Firefox
- Opera and
- IE.
In my case, I used Selenium mostly with Firefox.