2011-10-20 68 views
0

目前,有一款游戏拥有不同的群组,您可以每小时玩一次“黄金奖”。有时候有黄金,有时候没有。它每小时在facebook上发布“group2中的黄金”或“group6中的黄金”,而其他时间由于没有黄金作为该小时的奖品而没有帖子。我想写一个小脚本,每小时检查一次该网站,并获取结果(如果有或没有金子,以及什么组),然后显示给我。我想在Python中编写它,因为我正在学习它。这是最好的语言吗?我怎么去做这件事?我真正能够找到的是关于提取链接的信息。我不想提取链接,只是文本。感谢任何和所有的帮助。我很感激。从网站获取文本并将其显示回

回答

1

检出urllib2从网址获取HTML和BeautifulSoup/HTMLParser/etc来解析html。然后,你可以使用类似以此为出发点的脚本:

import time 
import urllib2 
import BeautifulSoup 
import HTMLParser 

def getSource(url, postdata): 
    source = "" 
    req = urllib2.Request(url, postdata) 
    try: 
     sock = urllib2.urlopen(req) 
    except urllib2.URLError, exc: 
     # handle the error.. 
     pass 
    else: 
     source = sock.read() 
    finally: 
     try: 
      sock.close() 
     except: 
      pass 
    return source 

def parseSource(source): 
    pass 
    # parse source with BeautifulSoup/HTMLParser, or here... 

def main(): 
    last_run = 0 
    while True: 
     t1 = time.time() 
     # check if 1 hour has passed since last_run 
     if t1 - last_run >= 3600: 
      source = getSource("someurl.com", "user=me&blah=foo") 
      last_run = time.time() 
      parseSource(source) 
     else: 
      # sleep for 60 seconds and check time again. 
      time.sleep(60) 
    return 0 

if __name__ == "__main__": 
    sys.exit(main()) 

这里是一个很好的文章有关parsing-html-with-python

+0

另请参阅:lxml.html – Lionel

1

我有类似的东西给你什么,而是你留下了什么我的主问题围绕着。我看着htmlparser和bs,但我不确定如何做一些事情,如if($ posttext == gold)echo“gold in so so so”..看起来像bs处理很多标签..我想因为facebook的帖子可以使用各种标签,我将如何去做只是对文本的搜索,并返回'后'?

相关问题