12

I'm trying to code a bot for a game, and need some help to do it. Being a complete noob, I googled how to do it with python and started reading a bit about mechanize.

<div class="clearfix">
                    <a href="#" onclick="return Index.submit_login('server_br73');">
            <span class="world_button_active">Mundo 73</span>
        </a>
                    </div>

My problem is in logging in, where i have this raw code for now:

import requests
import requesocks
import xlrd
import socks
import socket
import mechanize
import selenium
from bs4 import BeautifulSoup

# EXCEL
file_location = "/home/luis/Dropbox/Projetos/TW/multisbr.xlsx"
wb = xlrd.open_workbook(file_location)
sheetname = wb.sheet_names ()
sh1 = wb.sheet_by_index(0)

def nickNm():
    lista = [sh1.col_values(0, x) for x in range (sh1.ncols)]
    listaNomes = lista [1]
    x < 1
    print listaNomes
def passwd():
    lista = [sh1.col_values(1, x) for x in range (sh1.ncols)]
    listaPasswd = lista [1]
    x < 1
    print listaPasswd

# TOR

def create_connection(address, timeout=None, source_address=None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

# patch the socket module
socket.socket = socks.socksocket
socket.create_connection = create_connection

#BeautifulSoup

def get_source ():
    url = 'https://www.tribalwars.com.br'
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, 'lxml')

# ALFA
br = mechanize.Browser ()
twbr = 'https://www.tribalwars.com.br/index.php'

def alfa ():
    br.open(link)

    br.select_form(nr=0)
    br["user"] = "something"
    br["password"] = "pword"

    result = br.submit()

    br.geturl()


nickNm()
passwd()
alfa()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Luis Silva
  • 159
  • 4
  • and what is wrong with your code? what error message do you get? – tagoma Dec 19 '15 at 18:32
  • No errors. I just cant get around to press the button thats pops out after pressing login. The html in the begining is from the button im trying to press – Luis Silva Dec 19 '15 at 18:35
  • I think you may now remove the downvotes since the question was improved and now is clearer and has the necessary details. – alecxe Dec 30 '15 at 02:14
  • @LuisSilva have you considered switching to `selenium`? Thanks. – alecxe Dec 30 '15 at 19:58
  • So, when you say you are trying to "code a bot for a game", do you mean you are trying to create a script that will login and push buttons and such on a web-based game? – ThatAintWorking Jan 05 '16 at 20:50

1 Answers1

11

There is quite a lot of javascript involved when you perform different actions on a page, mechanize is not a browser and cannot execute javascript. One option to make your life easier here would be to automate a real browser. Here is an example code to log into the tribalwars using selenium and a headless PhantomJS:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("https://www.tribalwars.com.br/index.php")

# logging in
driver.find_element_by_id("user").send_keys("user")
driver.find_element_by_id("password").send_keys("password")
driver.find_element_by_css_selector("a.login_button").click()
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195