2012-02-10 14 views
0

我對網頁遊戲「Dragoncave」一個機器人。我的問題是,我不知道如何告訴它從索引到5個生物羣落中的一個(請參閱:http://dragcave.net)。的Python博特爲Browsergame-我需要一些幫助解決這個請

這是我有:

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:希望它可以理解嗎?

回答

0

你正在嘗試做一些稱爲屏幕抓取的操作。有這樣的圖書館,其中最受歡迎的是Beautiful Soup。嘗試查看文檔,並在那裏查看一些示例。你會有更愉快的時光。

+0

謝謝你,我會嘗試一下,我有BeautifulSoup已經我只需要找出如何去解決它,因爲我還是就在學習蟒蛇的開始^^ – Natalie 2012-02-11 09:04:58