2012-01-21 51 views
8

我使用lxml來解析頁面。當我運行我的代碼與App Engine SDK它的工作原理,但是當我在雲中部署我的應用程序,我在這裏得到一個messege:使用lxml在Google App引擎上導入錯誤

Traceback (most recent call last): File "/base/data/home/apps/s~testparsercyka/1.356245976008257055/handler_info.py", line 2, in import lxml.html File "/base/data/home/apps/s~testparsercyka/1.356245976008257055/lxml/html/init.py", line 12, in from lxml import etree ImportError: cannot import name etree

代碼:

的app.yaml



    application: testparsercyka 
    version: 1 
    runtime: python27 
    api_version: 1 
    threadsafe: false 

    handlers: 
    - url: /stylesheets 
     static_dir: stylesheets 

    - url: /.* 
     script: handler_info.py 

    libraries: 
    - name: lxml 
     version: "2.3" # I thought this would allow me to use lxml.etree 

handler_info.py



    import lxml 
    import lxml.html 
    import urllib 
    from google.appengine.ext import webapp 
    from google.appengine.ext.webapp.util import run_wsgi_app 
    from google.appengine.ext.webapp import template 
    import os 
    import cgi 
    class MainPage(webapp.RequestHandler): 
     def get(self): 
      template_values = {} 
      path = os.path.join(os.path.dirname(__file__), 'index.html') 
      self.response.out.write(template.render(path, template_values)) 
    class Handlers(webapp.RequestHandler): 
     def post(self): 
      #url = "http://habrahabr.ru/" 
      url = str(self.request.get('url')) 
      url_temp = url 
      teg = str(self.request.get('teg')) 
      attr = str(self.request.get('attr')) 
      n0 = str(self.request.get('n0')) 
      n = str(self.request.get('n')) 
      a = attr.split(':') 
      for i in range(int(n0),int(n)): 
       url = url.format(str(i)) 
       self.response.out.write(url) 
       html = urllib.urlopen(url).read()  
       doc = lxml.html.document_fromstring(html) 
       url = url_temp 
       self.getn(doc.getroottree().getroot(),teg,a) 
     def getn(self,node,teg,a): 
       if ((node.tag==teg) and (node.get(a[0])==a[1])): 
        #print node.tag,node.keys() 
        self.response.out.write(node.text) 
        self.response.out.write('
') for n in node: self.getn(n,teg,a) application = webapp.WSGIApplication([('/', MainPage),('/sign',Handlers)],debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()

任何想法爲什麼這不起作用?

+0

嗨artem,我有一個類似的問題,除了我在SDK中的時候出現導入錯誤,所以我還沒有嘗試過谷歌的服務器。您是否必須自己在計算機上安裝lxml?我想知道這是我的問題,但我認爲它會隨SDK一起提供... – Stin

回答

2

我知道這是一個老問題,但這裏是當部署到App Engine,我已確認工作答案:

的app.yaml

application: lxml-test 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: false 

handlers: 
- url: /.* 
    script: app.app 

libraries: 
- name: lxml 
    version: "2.3" 

- name: webapp2 
    version: "latest" 

app.py

import webapp2 
import lxml.etree 

class MainPage(webapp2.RequestHandler): 
    def get(self): 
     root = lxml.etree.XML('<top><content>Hello world!</content></top>') 
     self.response.content_type = 'text/xml' 
     self.response.write(lxml.etree.tostring(root, xml_declaration=True)) 

app = webapp2.WSGIApplication(routes=[('/', MainPage)], debug=True) 

因此,在比較以上與你的代碼,下面的一些變化米t幫助:

  1. 更改script: hander_info.pyscript: handler_info.application
  2. 使用webapp2比較好,比webapp更新。

從2012年起,當問題出現時,問題也可能自行解決。