Python 我想将数组传递到瓶子的模板并显示其内容

Python 我想将数组传递到瓶子的模板并显示其内容,python,python-3.x,beautifulsoup,bottle,Python,Python 3.x,Beautifulsoup,Bottle,环境 ・Python 3.6.0 ・瓶子0.13-dev ・mod_wsgi-4.5.15 在web上尝试以下代码会导致500个错误 500内部服务器错误 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) impo

环境

・Python 3.6.0

・瓶子0.13-dev

・mod_wsgi-4.5.15


在web上尝试以下代码会导致500个错误

500内部服务器错误


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)

视图/索引_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
[error]mod_wsgi(pid=23613):无法将目标wsgi脚本“/app.wsgi”作为Python模块加载。
[错误]mod_wsgi(pid=23613):处理wsgi脚本'/app.wsgi'时发生异常。
[错误]回溯(最近一次呼叫上次):
[错误]文件“/app.wsgi”,第8行,在
[错误]导入索引
[错误]文件“/index.py”,第11行
[错误]对于bsObj.findAll(“a”)中的链接:
[错误]^
[错误]缩进错误:意外缩进

日志报告了一个
IndentationError
,因此代码中的缩进有问题:具体地说,
for
循环缩进过度,for语句应该与
bsObj
赋值处于同一级别

您还需要使变量名保持一致(
link
|
links
),并使用
attrs
属性,而不是
attr
。修正了下面的代码(未测试)


日志报告了一个
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)