【问题标题】:Python Bot for Browsergame- I need some help fixing this please用于 Browsergame 的 Python Bot - 我需要一些帮助来解决这个问题
【发布时间】:2012-03-03 01:32:26
【问题描述】:

我有一个用于浏览器游戏“Dragoncave”的机器人。我的问题是,我不知道如何告诉它从索引到 5 个生物群落之一(见自己:http://dragcave.net)。

这就是我所拥有的:

import httplib, urllib

def login(): 
   id= raw_input("Please enter the cookie for scroll dragon[id]:")
   passw = raw_input("Please enter the cookie for pass (dragon[pass]:")
   return [id,passw]

def followLink(link,cookie): 
   con = httplib.HTTPConnection('dragcave.net/',80,timeout=2)
   con.putrequest('GET', link)
   con.putheader('Cookie', 'dragon[id]=' + cookie[0] +'; ' +'dragon[pass]=' + cookie[1] + '
check=1; referers=1; initjs=1; ads=0; pc=1; uc=0; ff=1;  _utma=164703850.337915616.1288510222.1288564452.1288567367.9');  
   con.putheader('Referer', 'http://dragcave.net/')
   con.putheader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12')
   con.endheaders()
   return con.getresponse()

def getData(cookie):     
   con = httplib.HTTPConnection('dragcave.net/',80,timeout=2)
   con.putrequest('GET', '/')
   con.putheader('Cookie', 'dragon[id]=' + cookie[0] +';' +'dragon[pass]=' + cookie[1])
   con.putheader('User-Agent', 'Mozilla/5.0 Gecko/20091221 Firefox/3.5.7')
   con.endheaders()
   response = con.getresponse()
   data = response.read()
   links = []
   texts = []
   links.append(data[data.index('/get'):data.index('/get') + 9])
   data = data[data.index('/get'):len(data)]
   texts.append(data[data.index('<br/>')+5:data.index('</div>')])
   data = data[data.index('</div>'):len(data)]
   links.append(data[data.index('/get'):data.index('/get') + 9])
   data = data[data.index('/get'):len(data)]
   texts.append(data[data.index('<br/>')+5:data.index('</div>')])
   data = data[data.index('</div>'):len(data)]
   links.append(data[data.index('/get'):data.index('/get') + 9])
   data = data[data.index('/get'):len(data)]
   texts.append(data[data.index('<br/>')+5:data.index('</div>')])
   data = data[data.index('</div>'):len(data)]
   con.close()
   return {texts[0]:links[0],texts[1]:links[1],texts[2]:links[2]}

import Autodragon,time,sys;
import webbrowser
dragon = raw_input("Enter the dragon codes. Put a #inbetween:").split("#")
location= raw_input("Enter the name of the locations. Put a #inbetween:").split("#")
cookie = Autodragon.login()
while 1:     
    try: 
        data = Autodragon.getData(cookie)
        print "Refresh"
        for d in dragon: 
            for t in data: 
                if  d == t: 
                    Autodragon.followLink(data[t], cookie)
                    webbrowser.open_new_tab("about:" + data[t][5:])
                    print d + " found"
    except:  
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        print exceptionType;    <br/>
        print exceptionValue
        print exceptionTraceback
        time.sleep(2)

有人可以帮我解决这个问题吗?我很抱歉无法按照应有的方式列出文本 D:无论如何希望它是可以理解的?

【问题讨论】:

    标签: python bots


    【解决方案1】:

    您正在尝试执行一种称为屏幕抓取的操作。有一些库,其中一个最受欢迎的是Beautiful Soup。尝试查看文档并在那里遵循一些示例。您将度过更愉快的时光。

    【讨论】:

    • 谢谢,我试试看,我已经有BeautifulSoup 我只需要了解如何使用它,因为我还在学习python的开始^^
    猜你喜欢
    • 2020-08-06
    • 2020-08-06
    • 2019-04-21
    • 2022-11-02
    • 2021-11-04
    • 1970-01-01
    • 2021-04-06
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多