我想将数组传递给瓶子的模板并显示其内容
I want to pass the array to the template of bottle and display its contents
环境
・Python 3.6.0
・瓶子 0.13-dev
・mod_wsgi-4.5.15
在网络上尝试以下代码会导致 500 个错误
500 Internal Server Error
app/wsgi
# -*- coding:utf-8 -*-
import sys, os
dirpath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(dirpath)
sys.path.append('../')
os.chdir(dirpath)
import bottle
import index
application = bottle.default_app()
index.py
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
@route('/')
@view("index_template")
def index():
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
bsObj = BeautifulSoup(html, "html.parser")
for link in bsObj.findAll("a"):
if 'href' in links.attr:
internalLinks.append(links.attr['href'])
return dict(internalLinks=internalLinks)
views/index_template.tpl
{{internalLinks}}
apache 日志
[error] mod_wsgi (pid=23613): Target WSGI script '/app.wsgi' cannot be loaded as Python module.
[error] mod_wsgi (pid=23613): Exception occurred processing WSGI script '/app.wsgi'.
[error] Traceback (most recent call last):
[error] File "/app.wsgi", line 8, in <module>
[error] import index
[error] File "/index.py", line 11
[error] for link in bsObj.findAll("a"):
[error] ^
[error] IndentationError: unexpected indent
日志报告 IndentationError
,因此您的代码中的缩进有问题:具体来说,for
循环过度缩进,for
语句应位于与 bsObj
作业相同级别。
您还需要使您的变量名称保持一致 (link
|links
) 并使用 attrs
属性,而不是 attr
。固定代码如下(未经测试)。
@route('/')
@view("index_template")
def index():
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
bsObj = BeautifulSoup(html, "html.parser")
for link in bsObj.findAll("a"):
if 'href' in link.attrs:
internalLinks.append(link.attrs['href'])
return dict(internalLinks=internalLinks)
环境
・Python 3.6.0
・瓶子 0.13-dev
・mod_wsgi-4.5.15
在网络上尝试以下代码会导致 500 个错误
500 Internal Server Error
app/wsgi
# -*- coding:utf-8 -*-
import sys, os
dirpath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(dirpath)
sys.path.append('../')
os.chdir(dirpath)
import bottle
import index
application = bottle.default_app()
index.py
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
@route('/')
@view("index_template")
def index():
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
bsObj = BeautifulSoup(html, "html.parser")
for link in bsObj.findAll("a"):
if 'href' in links.attr:
internalLinks.append(links.attr['href'])
return dict(internalLinks=internalLinks)
views/index_template.tpl
{{internalLinks}}
apache 日志
[error] mod_wsgi (pid=23613): Target WSGI script '/app.wsgi' cannot be loaded as Python module.
[error] mod_wsgi (pid=23613): Exception occurred processing WSGI script '/app.wsgi'.
[error] Traceback (most recent call last):
[error] File "/app.wsgi", line 8, in <module>
[error] import index
[error] File "/index.py", line 11
[error] for link in bsObj.findAll("a"):
[error] ^
[error] IndentationError: unexpected indent
日志报告 IndentationError
,因此您的代码中的缩进有问题:具体来说,for
循环过度缩进,for
语句应位于与 bsObj
作业相同级别。
您还需要使您的变量名称保持一致 (link
|links
) 并使用 attrs
属性,而不是 attr
。固定代码如下(未经测试)。
@route('/')
@view("index_template")
def index():
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
bsObj = BeautifulSoup(html, "html.parser")
for link in bsObj.findAll("a"):
if 'href' in link.attrs:
internalLinks.append(link.attrs['href'])
return dict(internalLinks=internalLinks)