我使用lxml来解析页面。当我使用app引擎sdk运行我的代码时,它可以工作,但是当我在云中部署我的应用程序时,我在这里获得了一个信息:
追踪(最近一次通话): 文件“/base/data/home/apps/s~testparsercyka/1.356245976008257055/handler_info.py”,第2行,in import lxml.html 文件“/base/data/home/apps/s~testparsercyka/1.356245976008257055/lxml/html/init.py”,第12行,在 来自lxml import etree ImportError:无法导入名称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 :(得分: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)
因此,在将上述内容与您的代码进行比较方面,以下某些更改可能有所帮助:
script: hander_info.py
更改为script: handler_info.application
。webapp
更新,更新的webapp2。自2012年提出这个问题以来,问题也有可能自行解决。