Уведомления

Группа в Telegram: @pythonsu

#1 Ноя. 30, 2014 20:31:36

DeathmasteR
От: Дзержинск
Зарегистрирован: 2014-11-30
Сообщения: 7
Репутация: +  0  -
Профиль   Адрес электронной почты  

Помогитите, пожалуйста! Syntax Error

Здравствуйте! Пытаюсь отладить бота для игры. IDLE выдаёт Syntax Error. Я в Питоне абсолютный новичок и не могу понять, что не так в коде. Помогите, пожалуйста!
Вот код:

# coding=utf-8
import urllib, re, sys, threading, cookielib, urllib2
from BeautifulSoup import BeautifulSoup
### Головная функция
def main():
    agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)'
    aCarnage = Carnage('usrlogin', 'userpassword', 'sarkel.carnage.ru', 'cp1251', agent)
    aCarnage.login()
    me = aCarnage.inf(aCarnage.user)
    # Если ранен - выйти
    if me['inj']:
        aCarnage.logout()
        exit(1)
    aCarnage.urlopen('main.pl')
    # Подождать пока здоровье восстановится
    while(me['hp_wait']):
        time.sleep(me['hp_wait'])
        me = aCarnage.inf(aCarnage.user)
    # Найти подходящую заявку
    aCarnage.find()
    me = aCarnage.inf(aCarnage.user)
    # Если заявка состоялась - в бой!
    if me['battle']: aCarnage.fight()
    # После боя - выход из игры
    aCarnage.logout()
class Opener:
    def __init__(self, host, encoding, agent):
        self.host = host
        self.encoding = encoding
        self.agent = agent
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    def urlopen(self, goto, data = None):
        f = self.opener.open(urllib2.Request(
            "http://%s/%s" % (self.host, goto),
            self.urlencode(data),
            {"User-agent" : self.agent}
        ))
        result = f.read()
        self.soup = BeautifulSoup(result)
    def urlencode(self, data):
        if data is None: return None
        for key in data:
            data[key] = data[key].encode(self.encoding, 'ignore')
        return urllib.urlencode(data)
class CarnageBot(Opener):
    ### Конструктор принимает логин, пароль, кодировку (cp1251) и идентификатор браузера
    def __init__(self, user, password, host, encoding, agent):
        self.user = user
        self.password = password
        Opener.__init__(self, host, encoding, agent)
    ### Получить информацию об игроке - например о самом себе
    def inf(self, user):
        self.urlopen('inf.pl?' + self.urlencode({'user': user}))
        # Кол-во жизни
        onmouseover = self.soup.find('img', onmouseover = re.compile(unicode('Уровень жизни:', 'utf8')))
        m = re.search('([0-9]+).([0-9]+)', onmouseover['onmouseover'])
        hp = int(m.group(1))
        hp_max = int(m.group(2))
        hp_wait = (100 - (hp * 100) / hp_max) * 18
        # Уровень
        td = self.soup.find('td', text = re.compile(unicode('Уровень:', 'utf8')))
        level = int(td.next.string)
        # Ранен или нет
        inj = 0
        if self.soup.find(src = re.compile(unicode('travma.gif', 'utf8'))): inj = 1
        # В бою или нет
        battle = 0
        if self.soup.find(text = re.compile(unicode('Персонаж находится в бою', 'utf8'))): battle = 1
        hero = {'level': level, 'hp': hp, 'hp_max': hp_max, 'hp_wait': hp_wait, 'inj': inj, 'battle': battle}
        return hero
    ### Войти в игру
    def login(self):
        data = {'action': 'enter', 'user_carnage': self.user, 'pass_carnage': self.password}
        self.urlopen('enter.pl', data)
        self.urlopen('main.pl')
    ### Выйти из игры
    def logout(self):
        self.urlopen('main.pl?action=exit')
    ### В бой!!!
    def fight(self):
        self.urlopen('battle.pl')
        while True:
            # Добить по таймауту
            if self.soup.find(text = re.compile(unicode('Противник потерял сознание', 'utf8'))):
                self.urlopen('battle.pl?cmd=timeout&status=win')
                break
            if self.soup.find(text = re.compile(unicode('Бой закончен.', 'utf8'))):
                break
            reg = re.compile(unicode('Для вас бой закончен. Ждите окончания боя.', 'utf8'))
            if self.soup.find(text = reg):
                break
            cmd = self.soup.find('input', {'name' : 'cmd', 'type' : 'hidden'})
            to = self.soup.find('input', {'name' : 'to', 'type' : 'hidden'})
            # Есть ли по кому бить?!
            if cmd and to:
                a = random.randint(1, 4)
                b0 = random.randint(1, 4)
                b1 = random.randint(1, 4)
                while b1 == b0: b1 = random.randint(1, 4)
                pos = 2
                arg = (cmd['value'], to['value'], pos, a, a, b0, b0, b1, b1)
                self.urlopen('battle.pl?cmd=%s&to=%s&pos=%s&A%s=%s&D%s=%s&D%s=%s' % arg)
            else:
                self.urlopen('battle.pl')
                time.sleep(random.randint(5, 30))
    ### Найти заявку - подающий заявку должен быть на 1 уровень ниже нашего
    def find(self):
        me = self.inf(self.user)
        while True:
            v = ''
            self.urlopen('zayavka.pl?cmd=haot.show')
            reg = re.compile(unicode('Текущие заявки на бой', 'utf8'))
            script = self.soup.find('fieldset', text = reg).findNext('script')
            m = re.findall('.*', script.string)
            for value in m:
                if value.find('group.gif') < 0: continue
                if value.find('(%i-%i)' % (me['level'] - 2, me['level'])) < 0: continue
                t = re.search(',([0-9]{1,2}),u', value)
                if not t: continue
                t = int(t.group(1))
                v = re.search('tr\(([0-9]+)', value).group(1)
                print 'Found battle t=%i v=%s' % (t, v)
                break
            if v: break
            time.sleep(80)
        nd = self.soup.find('input', {'name' : 'nd', 'type' : 'hidden'})
        self.urlopen('zayavka.pl?cmd=haot.accept&nd=%s&battle_id=%s' % (nd['value'], v))
        time.sleep(t + 30)
if __name__ == '__main__': main()

Вот скриншот места, где отладчик выдал ошибку:


Буду очень признателен за помощь!



Sic itur ad astra!

Офлайн

#2 Ноя. 30, 2014 20:41:58

terabayt
От: Киев
Зарегистрирован: 2011-11-26
Сообщения: 1099
Репутация: +  103  -
Профиль   Отправить e-mail  

Помогитите, пожалуйста! Syntax Error

может у вас 3 пайтон?! попробуйте так:

print('Found battle t=%i v=%s' % (t, v))



————————————————
-*- Simple is better than complex -*-

Офлайн

#3 Ноя. 30, 2014 21:02:23

DeathmasteR
От: Дзержинск
Зарегистрирован: 2014-11-30
Сообщения: 7
Репутация: +  0  -
Профиль   Адрес электронной почты  

Помогитите, пожалуйста! Syntax Error

terabayt
может у вас 3 пайтон?! попробуйте так:
Да у меня версия 3.4.2
Данная ошибка исчезла, но теперь

>>>
Traceback (most recent call last):
File “C:\bot.py”, line 2, in <module>
import urllib, re, sys, threading, cookielib, urllib2
ImportError: No module named ‘cookielib’
>>>

Можно ли из кода удалить данный модуль? Это не отразится на работе программы?



Sic itur ad astra!

Отредактировано DeathmasteR (Ноя. 30, 2014 21:11:09)

Офлайн

#4 Ноя. 30, 2014 21:13:30

terabayt
От: Киев
Зарегистрирован: 2011-11-26
Сообщения: 1099
Репутация: +  103  -
Профиль   Отправить e-mail  

Помогитите, пожалуйста! Syntax Error

The cookielib module has been renamed to http.cookiejar in Python 3.
можете в import убрать cookielib и с новой строки вставить
from http.cookiejar import CookieJar as cookielib
но потом полезет еще много ошибок.
вот можете посмотреть 2to3
или установите се второй пайтон



————————————————
-*- Simple is better than complex -*-

Офлайн

#5 Ноя. 30, 2014 22:50:16

DeathmasteR
От: Дзержинск
Зарегистрирован: 2014-11-30
Сообщения: 7
Репутация: +  0  -
Профиль   Адрес электронной почты  

Помогитите, пожалуйста! Syntax Error

Поставил 2,7, выдаёт

Python 2.7.7 (default, Jun 1 2014, 14:21:57) on win32
Type “copyright”, “credits” or “license()” for more information.
>>> ================================ RESTART ================================
>>>

Traceback (most recent call last):
File “C:\bot.py”, line 3, in <module>
from BeautifulSoup import BeautifulSoup
ImportError: No module named BeautifulSoup
>>>



Sic itur ad astra!

Офлайн

#6 Ноя. 30, 2014 22:58:52

terabayt
От: Киев
Зарегистрирован: 2011-11-26
Сообщения: 1099
Репутация: +  103  -
Профиль   Отправить e-mail  

Помогитите, пожалуйста! Syntax Error

BeautifulSoup



————————————————
-*- Simple is better than complex -*-

Офлайн

#7 Ноя. 30, 2014 23:09:27

DeathmasteR
От: Дзержинск
Зарегистрирован: 2014-11-30
Сообщения: 7
Репутация: +  0  -
Профиль   Адрес электронной почты  

Помогитите, пожалуйста! Syntax Error

terabayt
BeautifulSoup
У меня 4.0.1 стояла. Сейчас попробую. Спасибо!



Sic itur ad astra!

Офлайн

#8 Ноя. 30, 2014 23:16:46

DeathmasteR
От: Дзержинск
Зарегистрирован: 2014-11-30
Сообщения: 7
Репутация: +  0  -
Профиль   Адрес электронной почты  

Помогитите, пожалуйста! Syntax Error

terabayt
BeautifulSoup

Так поставил. Теперь вот это:

Python 2.7.7 (default, Jun 1 2014, 14:21:57) on win32
Type “copyright”, “credits” or “license()” for more information.
>>> ================================ RESTART ================================
>>>

Traceback (most recent call last):
File “C:\bot.py”, line 129, in <module>
if __name__ == ‘__main__’: main()
File “C:\bot.py”, line 7, in main
aCarnage = Carnage('Сумрачный Ветер', ‘devastator’, ‘sarkel.carnage.ru’, ‘cp1251’, agent)
NameError: global name ‘Carnage’ is not defined
>>>
Может из-за того что логин на кириллице?



Sic itur ad astra!

Офлайн

#9 Ноя. 30, 2014 23:31:53

terabayt
От: Киев
Зарегистрирован: 2011-11-26
Сообщения: 1099
Репутация: +  103  -
Профиль   Отправить e-mail  

Помогитите, пожалуйста! Syntax Error

aCarnage = Carnage('YourNick', 'YourPass', 'arkaim.carnage.ru', 'cp1251', agent)
замените на
aCarnage = CarnageBot('YourNick', 'YourPass', 'arkaim.carnage.ru', 'cp1251', agent)



————————————————
-*- Simple is better than complex -*-

Офлайн

#10 Ноя. 30, 2014 23:38:06

DeathmasteR
От: Дзержинск
Зарегистрирован: 2014-11-30
Сообщения: 7
Репутация: +  0  -
Профиль   Адрес электронной почты  

Помогитите, пожалуйста! Syntax Error

Теперь с кодировкой проблемы :
>>>

Traceback (most recent call last):
File “C:\bot.py”, line 129, in <module>
if __name__ == ‘__main__’: main()
File “C:\bot.py”, line 8, in main
aCarnage.login()
File “C:\bot.py”, line 74, in login
self.urlopen('enter.pl', data)
File “C:\bot.py”, line 35, in urlopen
self.urlencode(data),
File “C:\bot.py”, line 43, in urlencode
data = data.encode(self.encoding, ‘ignore’)
File “C:\Python27\lib\encodings\cp1251.py”, line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeDecodeError: ‘ascii’ codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
>>>



Sic itur ad astra!

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version