Python浏览器游戏机器人 - 请帮我修复这个问题
我有一个用于浏览器游戏“Dragoncave”的机器人。我的问题是,我不知道怎么让它从首页跳转到五个不同的生态区之一(你可以自己看看: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: 希望这样也能让人理解?
1 个回答
0
你正在尝试做一种叫做“屏幕抓取”的事情。为了实现这个功能,有一些专门的工具库,其中一个最受欢迎的叫做 Beautiful Soup。你可以去看看它的使用说明,跟着里面的一些例子来做。这样你会觉得过程更轻松有趣。