2013-05-03 249 views
0

想要使用Python從特定網站獲取數據以上傳到Google App Engine。這個想法是創建一個數據庫數據來存儲服務器上的信息,以檢索並顯示在Web應用程序中。使用Python從網頁獲取數據

from google.appengine.ext import webapp 
    from google.appengine.ext.webapp.util import run_wsgi_app 
    from google.appengine.ext import db 
    import os 
    import datetime 
    from google.appengine.ext.webapp import template 


# A class which creates all the pokemon on the server 
class Pokemondata(db.Model): 
number = db.IntegerProperty() 
pokemonname = db.StringProperty() 
description = db.StringProperty() 

newpoke = Pokemondata(number="001",pokemonname="Balbasuar",description="The grass  pokemon") 
newpoke = Pokemondata(number="002",pokemonname="Ivysaur",description="The seed pokemon") 
newpoke = Pokemondata(number="003",pokemonname="Venasaur",description="Another grass pokemon") 
newpoke.put() 

    # A class to put new pokemon in to the server ? 
class ApplyHandler(webapp.RequestHandler): 
    def post(self): 
    self.session = Session() 
    pnumber = self.request.get('number') 
    pname = self.request.get('pokemonname') 
    pdescription = self.request.get('description') 

    newpoke = Pokemondata(number=pnumber,pokemonname=pname,description=pdescription) 
newpoke.put() 
    self.session['pokemon'] = pname 
    doRender(self,"board.htm",{}) 

    # Construct a google table for this data 
    # to display 
    class JSONInterface(webapp.RequestHandler): 
    def get(self): 
     que = db.Query(Pokemondata) 
     listing = que.fetch(limit = 12) 
     doRender(self,'http://pokedexapp.appspot.com/[email protected]#input', 
     {'listing':listing}) 


    application = webapp.WSGIApplication([('/(.*html)?', ApplyHandler), 
    ('/json', JSONInterface)], debug=True) 


    def main(): 
    run_wsgi_app(application) 
    if __name__ == "__main__": 
     main() 

當我部署應用程序來測試我得到服務器錯誤。任何人都可以告訴我這是否是正確方向的一步,需要修改哪些內容?

YAML的文件是目前

應用:pokedexapp 版本:1 運行:蟒蛇 API_VERSION:1

處理程序: - 網址:/.* 腳本:main.py

+0

我明白代碼縮進是錯誤的。我目前正在嘗試修復它 – user2045126 2013-05-03 02:55:06

+0

你想發佈你得到的錯誤。 – dragonx 2013-05-03 03:35:57

+0

你可以發佈.yaml文件嗎?我在這裏看到一些錯誤,在處理程序中使用的MainHandler,JSONInterface,SearchResult類在哪裏? – marcadian 2013-05-03 05:27:52

回答

0
from google.appengine.ext import db 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp import template 
# A class which creates all the pokemon on the server 
class Pokemondata(db.Model): 
number = db.IntegerProperty() 
pokemonname = db.StringProperty() 
description = db.StringProperty() 


def post(self): 
self.session = Session() 
pnumber = self.request.get('number') 
pname = self.request.get('pokemonname') 
pdescription = self.request.get('description') 


newpoke = Pokemondata(number=pnumber,pokemonname=pname,description=pdescription) 
newpoke.put() 
self.session['pokemon'] = pname 
doRender(self,"board.htm",{}) 


Page = """ 
<html manifest="my.manifest"> 
<head> 
     <!-- All the js and css header links go here --> 
</head> 
<body> 
     <!-- all the needed HTML markup --> 

     <!-- if there is a place you would need to embed values (e.g. a user name), just use %s in its place, e.g. --> 
<h1>%s's App Engine App</h> 
     <!-- you can now do a string substitution for the each %s (in order) - see end… 
</body> 
</html> 
    """  


# A class to get/put pokemon in to the server ? 
class ApplyHandler(webapp.RequestHandler): 
    def post(self): 
     self.session = Session() 
    pnumber = self.request.get('number') 
    pname = self.request.get('pokemonname') 
    pdescription = self.request.get('description') 

    newpoke = Pokemondata(number=pnumber,pokemonname=pname,description=pdescription) 
    newpoke.put() 
    self.session['pokemon'] = pname 
    doRender(self,"board.htm",{}) 

# Construct a google big ass table for this data 
# to display 

    def get(self): 
     que = db.Query(Pokemondata) 
    listing = que.fetch(limit = 12) 
    doRender(self,'http://pokedexapp.appspot.com/[email protected]#input', 
      {'listing':listing})  

    #self.response.out.write(page % "Fred Bloggs") 
    # This is standard Google App Engine stuff - create a WSGI application 
    # framework and set up the routing between URIs and Handlers... 
application = webapp.WSGIApplication([('/', Pokemondata), 
             ('/json', JSONInterface)], debug=True) 
def Addlist(): 
    run_wsgi_app(application) 

if __name__ == "__Addlist__": 
    main() 
    code here 
+0

我試過使用該示例,但我仍然收到錯誤。 – user2045126 2013-05-03 18:12:24

+0

這個例子應該做什麼? – FDinoff 2013-05-03 18:13:30

+0

該示例應該獲取代碼中創建的數據並將其發佈到服務器... Html已被排除用於格式化輸出,但它只是顯示數據庫條目的結果 – user2336427 2013-05-03 18:48:19

0
from google.appengine.ext import db 

# A class which creates all the pokemon on the server 
class Pokemondata(db.Model): 
pokemonname = db.StringProperty() 

newpoke = Pokemondata(pokemonname="Balbasuar") 
newpoke.put() 

class JSONInterface(webapp.RequestHandler): 
    def post(self): 
     pkmname = self.request.get('pokemonname') 
     callback = self.request.get('callback') 
     # This line makes the Message object... 
     pkmname = Pokemondata(pokemonname=pkmname) 
     # This one pushes it to the database (a Big-Table)... 
     pkmname.put() 
     self.response.out.write(getJSONMessages(callback)) 

    def get(self): 
     callback = self.request.get('callback') 
     self.response.out.write(getJSONMessages(callback)) 
0
from google.appengine.ext import db 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext import db 
import os 
import datetime 
from google.appengine.ext.webapp import template 

# A class which creates all the pokemon on the server 
class Pokemondata(db.Model): 
    def get(self): 
     pokemonname = db.StringProperty() 
     newpoke = Pokemondata(pokemonname="Balbasuar") 
     newpoke.put() 

class JSONInterface(webapp.RequestHandler): 

    def post(self): 
     pkmname = self.request.get('pokemonname') 
     callback = self.request.get('callback') 
     # This line makes the Message object... 
     pkmname = Pokemondata(pokemonname=pkmname) 
     # This one pushes it to the database (a Big-Table)... 
     pkmname.put() 
     self.response.out.write(getJSONMessages(callback)) 

    def get(self): 
     callback = self.request.get('callback') 
     self.response.out.write(getJSONMessages(callback))